note 91721 added to function.date
| From: | kyperkins | Date: | Wed, 24 Jun 2009 02:34:24 +0000 |
| Subject: | note 91721 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157319@lists.php.net to get a copy of this message | ||
After searching and searching for a working DateDiff function that actually calculated the
difference between two dates AND times, I finally came up with my own which I used a lot of
different ideas from around the web, but this works for calculating the difference between two dates
and times, and put the output into something readable.
<?php
#2DateTimeDiff Script by Kyle Perkins
#kyperkins@shadowdev.com
#06/23/09
#START
#$date2 must be in Y-m-d-H-i-s format for this to work (concatinate if you need to)
#get the numbers in their own variables
$now_date=date("Y-m-d-H-i-s");
list($now_year, $now_month, $now_day, $now_hour, $now_minute, $now_second) =
explode("-",$now_date);
list($date2_year, $date2_month, $date2_day, $date2_hour, $date2_minute, $date2_second) =
explode("-",$date2_date);
#get the two dates and times in Unix Timestamp format
$now=time();
$ends=mktime($date2_hour, $date2_minute, $date2_second, $date2_month, $date2_day, $date2_year);
#do the calculations and get the difference
$diff_time=$ends-$now;
#put the difference into a nice readable format
$diff_time_nice=date("Y-n-j-G-i-s", $diff_time);
$diff_t=explode("-", $diff_time_nice);
list($difference_year, $difference_month, $difference_day, $difference_hour, $difference_minute,
$difference_second) = $diff_t;
#error trap for 1970 UNIX Timestamp issue
$difference_year=1970-$difference_year;
#fix the "05 Minutes" issue, where the minute indicator has a 0. We don't want a 0
if ($difference_minute[0]=="0") $difference_minute=substr($difference_minute, 1);
#simple function: adds an "s" if there is more than 1 ### left
function finds($time){
global $diff;
if ($time>1) $diff.= "s";
}
#concatinate to the $diff variable for the time remaning
if ($difference_year){
$diff= "$difference_year year";
finds($difference_year);
}
if ($difference_month){
$diff.= ", $difference_month month";
finds($difference_month);
}
if ($difference_day) {
$diff.= ", $difference_day day";
finds($difference_day);
}
if ($difference_hour) {
$diff.= ", $difference_hour hour";
finds($difference_hour);
}
if ($difference_minute) {
$diff.= ", $difference_minute minute";
finds($difference_minute);
}
if ($difference_second) {
$diff.= ", $difference_second second";
finds($difference_second);
}
if ($diff[0]==",") $diff=substr($diff, 2);
#echo the result
echo "$diff"; #echos something like "3 years, 2 months, 1 day, 3 hours, 2
minutes, 1 second". The rest is up to you
?>
----
Server IP: 208.69.120.35
Probable Submitter: 72.71.126.221
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/91721
Del: integrated -- https://master.php.net/note/delete/91721/integrated
Del: useless -- https://master.php.net/note/delete/91721/useless
Del: bad code -- https://master.php.net/note/delete/91721/bad+code
Del: spam -- https://master.php.net/note/delete/91721/spam
Del: non-english -- https://master.php.net/note/delete/91721/non-english
Del: in docs -- https://master.php.net/note/delete/91721/in+docs
Del: other reasons-- https://master.php.net/note/delete/91721
Reject -- https://master.php.net/note/reject/91721
Search -- https://master.php.net/manage/user-notes.php