Формат даты mysql в формат php

PHP date() format when inserting into datetime in MySQL

What is the correct format to pass to the date() function in PHP if I want to insert the result into a MySQL datetime type column?

I’ve been trying date(‘Y-M-D G:i:s’) but that just inserts «0000-00-00 00:00:00» everytime.

13 Answers 13

Try: date(‘Y-m-d H:i:s’) which uses the numeric equivalents.

From the comments of php’s date() manual page:

Here’s an alternative solution: if you have the date in PHP as a timestamp, bypass handling it with PHP and let the DB take care of transforming it by using the FROM_UNIXTIME function.

I use the following PHP code to create a variable that I insert into a MySQL DATETIME column.

This will hold the server’s current Date and Time.

I use this function (PHP 7)

Older versions of PHP (PHP

Format time stamp to MySQL DATETIME column :

Format MySQL datetime with PHP

There is no need no use the date() method from PHP if you don’t use a timestamp. If dateposted is a datetime column, you can insert the current date like this:

This has been driving me mad looking for a simple answer. Finally I made this function that seems to catch all input and give a good SQL string that is correct or at least valid and checkable. If it’s 1999-12-31 it’s probably wrong but won’t throw a bad error in MySQL.

Note: I understood the question referring to current time.

Источник

Читайте также:  True rms мультиметр что это
Образовательный портал