Php generates date / time in the same format as mysql datatime

I would like to know if there is a function in php to create a data / time format like in mysql: 0000-00-00 00:00:00. I know that you can generate data / time in a sql query using CURDATE (), but I would like to know if it is possible to generate it using php in mysql format in the same format.

+4
source share
3 answers

Sure! Give it a go date('Ymd H:i:s')

+4
source

PHP currently only supports RFC 2822 and ISO 8601 compliance dates, as well as Unix timestamps. More details here .

The reason for this is probably because they include a time zone offset, and the β€œmysql form” - its localized compilation format - doesnt, which often leads to confusion when used with application servers.

But you can define your own formats as you wish, all components are available through the datetime functions. Just look here and here.

Example:

 $mysql_date = date('Ymd H:i:s', $unix_timestamp); 

But remember the Y2k38 bug , if you are not using a 64-bit system, you can use the datetime class

0
source

This should convert the standard formats:

 $format = 'DATETIME'; $format = str_replace( array('DATETIME', 'DATE', 'TIME', 'YEAR'), array('DATE TIME', 'Ym-d', 'H:i:s', 'Y'), $format); echo date($format, time()); 
0
source

Source: https://habr.com/ru/post/1336054/


All Articles