略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: 日期/时间

2024-12-21

日期和时间

add a noteadd a note

User Contributed Notes 2 notes

up
6
Moo0z0r
12 years ago
I think it's important to mention with the DateTime class that if you're trying to create a system that should store UNIX timestamps in UTC/GMT, and then convert them to a desired custom time-zone when they need to be displayed, using the following code is a good idea:

<?php
date_default_timezone_set
('UTC');
?>

Even if you use something like:

<?php
$date
->setTimezone( new DateTimeZone('UTC') );
?>

... before you store the value, it doesn't seem to work because PHP is already trying to convert it to the default timezone.
up
0
theking2 at king dot ma
2 months ago
If working with SQL databases it is important to realize that where DB normally reserve 0000-00-00 as a special value for indicating not valid date.

If this was converted in PHP, unexpected things happen.

<?php

$s
= "0000-00-00 00:00:00";
$d = \DateTime::createFromFormat('Y-m-d H:i:s',$s);
echo
$d-> format("d.m.Y H:i:s");

// 30.11.-0001 00:00:00
?>

Available on https://3v4l.org/jnoIJ

官方地址:https://www.php.net/manual/en/book.datetime.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3