Convert from MySQL Datetime to Another Format With PHP

Convert from MySQL datetime to another format with PHP

If you're looking for a way to normalize a date into MySQL format, use the following

$phpdate = strtotime( $mysqldate );
$mysqldate = date( 'Y-m-d H:i:s', $phpdate );

The line $phpdate = strtotime( $mysqldate ) accepts a string and performs a series of heuristics to turn that string into a unix timestamp.

The line $mysqldate = date( 'Y-m-d H:i:s', $phpdate ) uses that timestamp and PHP's date function to turn that timestamp back into MySQL's standard date format.

(Editor Note: This answer is here because of an original question with confusing wording, and the general Google usefulness this answer provided even if it didnt' directly answer the question that now exists)

How to convert MySQL DateTime to PHP Y-m-d?

$formattedDate = date("Y-m-d", strtotime($mysqlDate));

if (strtotime($somedate) >strtotime($someotherdate)){
// do stuff here
}

strtotime standardizes most dates and times into the number of seconds since jan1 1970.

Convert one date format into another in PHP

The second parameter to date() needs to be a proper timestamp (seconds since January 1, 1970). You are passing a string, which date() can't recognize.

You can use strtotime() to convert a date string into a timestamp. However, even strtotime() doesn't recognize the y-m-d-h-i-s format.

PHP 5.3 and up

Use DateTime::createFromFormat. It allows you to specify an exact mask - using the date() syntax - to parse incoming string dates with.

PHP 5.2 and lower

You will have to parse the elements (year, month, day, hour, minute, second) manually using substr() and hand the results to mktime() that will build you a timestamp.

But that's a lot of work! I recommend using a different format that strftime() can understand. strftime() understands any date input short of the next time joe will slip on the ice. for example, this works:

$old_date = date('l, F d y h:i:s');              // returns Saturday, January 30 10 02:06:34
$old_date_timestamp = strtotime($old_date);
$new_date = date('Y-m-d H:i:s', $old_date_timestamp);

how to convert date format to datetime format using php

Why don't you use the DateTime object in PHP (http://php.net/manual/en/class.datetime.php)?

Here is how i would do it:

$date = DateTime::createFromFormat('d/m/Y', $travelDate);
echo $date->format('Y-m-d H:i:s');

Hope it helps.

How can I Change datetime format with PHP

Use google, couple of examples:

result = mysql_query("SELECT `datetime` FROM `table`");
$row = mysql_fetch_row($result);
$date = date_create($row[0]);

echo date_format($date, 'Y-m-d H:i:s');
#output: 2012-03-24 17:45:12

echo date_format($date, 'd/m/Y H:i:s');
#output: 24/03/2012 17:45:12

echo date_format($date, 'd/m/y');
#output: 24/03/12

echo date_format($date, 'g:i A');
#output: 5:45 PM

echo date_format($date, 'G:ia');
#output: 05:45pm

echo date_format($date, 'g:ia \o\n l jS F Y');
#output: 5:45pm on Saturday 24th March 2012

How to convert a mysql datetime in PHP to m/d/y format?

I think what you really want is this:

$fromMYSQL = '2007-10-17 21:46:59';
echo date("m/d/Y", strtotime($fromMYSQL));

The second argument of date is a timestamp and I think what is happening is PHP sees your string as a -1 timestamp... thus 12/31/1969.

So, to get a timestamp from the string version of the date, you use strtotime

Convert specific time format to in PHP to MYSQL datetime type

You can use date function of php.

echo date("Y-m-d h:i:s",strtotime($yourDateTimeVariable));

for more help see php manual for date.

http://php.net/manual/en/function.date.php

Hope it helps

Convert a datetime format to match MySQL's datetime with PHP

The problem appears to be the trailing zeroes, by trimming them from the date string it seems to work fine. The code below uses one of the Date constants - change that for the format desired.

$d='Tue, 20 Oct 2015 17:43:23 0000';
echo date( DATE_COOKIE, strtotime( trim($d,' 0000') ) );
outputs -> Tuesday, 20-Oct-15 17:43:23 BST

echo date( 'Y-m-d H:i:s', strtotime( trim($d,' 0000') ) );
outputs -> 2015-10-20 17:43:23


Related Topics



Leave a reply



Submit