datetime - Convert one date format into another in PHP -


is there simple way convert 1 date format date format in php?

i have this:

$old_date = date('y-m-d-h-i-s');            // works  $middle = strtotime($old_date);             // returns bool(false)  $new_date = date('y-m-d h:i:s', $middle);   // returns 1970-01-01 00:00:00 

but i'd of course return current date rather crack 'o dawn. doing wrong?

the second parameter date() needs proper timestamp (seconds since january 1, 1970). passing string, date() can't recognize.

you can use strtotime() convert date string timestamp. however, strtotime() doesn't recognize y-m-d-h-i-s format.

php 5.3 , up

use datetime::createfromformat. allows specify exact mask - using date() syntax - parse incoming string dates with.

php 5.2 , lower

you have parse elements (year, month, day, hour, minute, second) manually using substr() , hand results mktime() build timestamp.

but that's lot of work! recommend using different format strftime() can understand. strftime() understands any date input short of the next time joe slip on ice. example, 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);    

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -