note 51079 added to ref.datetime

From: Date: Fri, 18 Mar 2005 23:19:13 +0000
Subject: note 51079 added to ref.datetime
Groups: php.notes 
Request: Send a blank email to php-notes+get-86667@lists.php.net to get a copy of this message
This is a litttle function I cobbled together from my own code, and snippets from this site to calculate the difference between two datetimes without having to confine it to simply one interval. This will tell you how many weeks, days, hours, minutes, and seconds there are between the given datetimes, and also makes a little English string you can use. This could easily be expanded to include months, and years, but I didn't want to have to deal with any of the leap year and variable month length stuff. <? function dateDiff($dateTimeBegin,$dateTimeEnd) { $dateTimeBegin =strtotime($dateTimeBegin); $dateTimeEnd =strtotime($dateTimeEnd); if($dateTimeEnd === -1 || $dateTimeBegin === -1) { # error condition return false; } $diff = $dateTimeEnd - $dateTimeBegin; if ($diff < 0) { # error condition return false; } $weeks = $days = $hours = $minutes = $seconds = 0; # initialize vars if($diff % 604800 > 0) { $rest1 = $diff % 604800; $weeks = ($diff - $rest1) / 604800; # seconds a week if($rest1 % 86400 > 0) { $rest2 = ($rest1 % 86400); $days = ($rest1 - $rest2) / 86400; # seconds a day if( $rest2 % 3600 > 0 ) { $rest3 = ($rest2 % 3600); $hours = ($rest2 - $rest3) / 3600; # seconds an hour if( $rest3 % 60 > 0 ) { $seconds = ($rest3 % 60); $minutes = ($rest3 - $seconds) / 60; # seconds a minute } else { $minutes = $rest3 / 60; } } else { $hours = $rest2 / 3600; } } else { $days = $rest1/ 86400; } }else { $weeks = $diff / 604800; } $string = array(); if($weeks > 1) { $string[] = "$weeks weeks"; } elseif ($weeks == 1) { $string[] = "a week"; } if($days > 1) { $string[] = "$days days"; } elseif($days == 1) { $string[] = "a day"; } if($hours > 1) { $string[] = "$hours hours"; } elseif ($hours == 1) { $string[] = "an hour"; } if($minutes > 1) { $string[] = "$minutes minutes"; } elseif ($minutes == 1) { $string[] = "a minute"; } if($seconds > 1) { $string[] = "$seconds seconds"; } elseif($seconds == 1) { $string[] = "a second"; } # join together all the strings in the array above except the last element $text = join(', ', array_slice($string,0,sizeof($string)-1)) . ", and "; $text .= array_pop($string); # put the last one on after the and return array($text, $weeks, $days, $hours, $minutes, $seconds); ?> ---- Manual Page -- http://www.php.net/manual/en/ref.datetime.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+51079 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51079&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51079&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51079&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51079&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51079&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+51079&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#86667) next »