Timestamp to normal date format and reverse conversion using PHP

22,296

Solution 1

$converted = date('d M Y h.i.s A', strtotime('2011-08-27 18:29:31'));
$reversed = date('Y-m-d H.i.s', strtotime($converted));

Solution 2

Don't use date()! It`s too old function. In PHP v. 5.2 and more you should use date_format or DateTime::format object.

Solution 3

you can use the date_format() function

//Convert to format: 27 Aug 2011 06.29.31 PM
$converted_date = date_format('d M Y h.i.s A',strtotime($orig_date));

//Convert to format 2011-08-27 18:29:31
$converted_date = date_format('Y-m-d H:i:s',strtotime($orig_date));
Share:
22,296
Alfred
Author by

Alfred

I am a Full Stack developer and a DevOps Engineer, who always to learn from, and contribute to, the technology community. I was a beginner in programming once. What all I had was pieces of scattered and crude knowledge (don't know if i can call it knowledge) then. Later, I joined for Master of Computer Applications in a college and there, I got a great teacher. It was her, who taught me when and where to use 'while' and 'for' loops even.What all knowledge I have now, and what all achievements I've made till now, is only because of her. Compared to her, I am ashes. Hats off my dear teacher Sonia Abraham, you are the best of your kind. Sonia Abraham is a professor of the Department of Computer Applications, M.A College of Engineering, Mahatma Gandhi University

Updated on February 23, 2020

Comments

  • Alfred
    Alfred about 4 years

    I have a timestamp as 2011-08-27 18:29:31. I want to convert it to 27 Aug 2011 06.29.31 PM. Also, I want to convert this format reverse back to the previous timestamp format. How van I do this using PHP?