I am getting time in the following format as a response of an API call. How I can I convert it to Y-m-d H:i:s I used the following :
$time_other_format = '2013-10-29T08:34:01-0700';
$time = date("Y-m-d H:i:s",strtotime($time_other_format));
This is returning the time, but the time is changed and I think as per the timezone. I have records in DB which are inserted before the convesrion and so retain the original time and to compare it. How can I convert it like that . I mean I need to get the time as 08:34:01 after converting it
Example of Timestamp to Date in PHP We are converting timestamp to date. echo date( "Y-m-d H:i:s" , 1620790172 ); echo " , "; echo date( "d-m-Y H:i:s" , 1620790172 );
Answer: Use the PHP date() Function You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.
php $date=strtotime("tomorrow"); echo date("Y-m-d h:i:sa", $date) . "<br>"; $date=strtotime("next Sunday"); echo date("Y-m-d h:i:sa", $date) . "<br>"; $date=strtotime("+3 Months"); echo date("Y-m-d h:i:sa", $date) .
$time_other_format = '2013-10-29T08:34:01-0700';
$dt = new DateTime($time_other_format);
echo $dt->format('Y-m-d H:i:s');
This will echo,
2013-10-29 08:34:01
The DateTime class in PHP is very powerful, and will recognise almost any format you throw at it. It's great for manipulation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With