Posted By

WouterJ on 06/25/12


Tagged

date functions convert


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Some0ne


Timestamp2date


 / Published in: PHP
 

Timestamp2array

Convert a timestamp (20120525) to a date (2012-05-25), you can specify the format in the second parameter.

  1. /**
  2.  * Convert a timestamp (number) to a date
  3.  *
  4.  * @param mixed $timestamp A timestamp i.e. 20120525
  5.  * @param string $format The format of the date
  6.  *
  7.  * @return string
  8.  */
  9. function timestamp2date($timestamp, $format = 'Y-m-d')
  10. {
  11. $year = substr($timestamp, 0, 4);
  12. $month = substr($timestamp, 4, 2);
  13. $day = substr($timestamp, 6, 2);
  14.  
  15. $date = new Datetime($year.'-'.$month.'-'.$day);
  16.  
  17. return $date->format($format);
  18. }
  19.  
  20. /* Example
  21.  * =======
  22.  */
  23. $timestamp = 20120525;
  24.  
  25. $date = timestamp2date($timestamp); // 2012-05-25

Report this snippet  

You need to login to post a comment.