Snippets.PRO Keep. Track. Share.

Have a Snippet?

Keep, track and share your code snippets with your friends



PHP: Convert the date interval to human-readable format Share on Vkontakte

Convert the date/time interval in human-readable format

  1. $startDate = 'now';
  2. $endDate = '2013-08-12 14:30:00';
  3.  
  4. function date_diff($date1, $date2) {
  5. $diff = strtotime($date2) - strtotime($date1);
  6. return abs($diff);
  7. }
  8.  
  9. function seconds2human($ss) {
  10. $s = $ss % 60;
  11. $m = floor(($ss % 3600) / 60);
  12. $h = floor(($ss) / 3600);
  13.  
  14. return "$h hours, $m minutes, $s seconds";
  15. }
  16.  
  17. echo "This file will expire in ". seconds2human(date_diff($startDate,$endDate));


Tag: PHP, Date Routines, DateTime, readable

0 Comments