开发者

php generate date/ time in the same format appears mysql datatime

开发者 https://www.devze.com 2023-02-05 14:35 出处:网络
I would like to know if there is a function in php to generate data/time format like in mysql : 0000-00-00 00:00:00 .

I would like to know if there is a function in php to generate data/time format like in mysql : 0000-00-00 00:00:00 . I know you can generate data/time in the sql query with CURDATE() but I would like to know if it's possible to generate i开发者_JAVA百科t with php in the same format mysql datatime format .


Sure is! Give this a go date('Y-m-d H:i:s')


At the moment PHP only supports RFC 2822 and ISO 8601 conform dates as well as Unix epoch timestamp. Check more here.

The reason for that is probably that they include timezone offset, and the "mysql form" - its a localized compbound format - doesnt, which would often lead to a mess in when using with application servers.

But you can define your own formats as you like, all the components are available via the datetime functions. Just have a look here and here

Example:

$mysql_date = date('Y-m-d H:i:s', $unix_timestamp);

But be aware of the Y2k38 bug if you are not on a 64 bit system, you may want to use the datetime class


This should convert the standard formats:

$format = 'DATETIME';

$format = str_replace(
    array('DATETIME', 'DATE', 'TIME', 'YEAR'),
    array('DATE TIME', 'Y-m-d', 'H:i:s', 'Y'),
$format);

echo date($format, time());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号