note 20922 added to function.date
| From: | ecc at gmanetwork dot com | Date: | Mon, 22 Apr 2002 07:59:54 +0000 |
| Subject: | note 20922 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-29668@lists.php.net to get a copy of this message | ||
to compute for the duration between two dates, you may use the script below:
$a[end_date] = '2003-03-01 00:00:00';
$a[start_date] = '2003-02-27 00:59:59';
$tok13 = strtok($a[start_date],"-");
$n13=1;
while ($tok13) {
$dte13[$n13]=$tok13;
$tok13 = strtok("-");
$n13++;
}
$tok14 = strtok($dte13[3]," ");
$n14=1;
while ($tok14) {
$dte14[$n14]=$tok14;
$tok14 = strtok(" ");
$n14++;
}
$tok15 = strtok($dte14[2],":");
$n15=1;
while ($tok15) {
$dte15[$n15]=$tok15;
$tok15 = strtok(":");
$n15++;
}
$tok16 = strtok($a[end_date],"-");
$n16=1;
while ($tok16) {
$dte16[$n16]=$tok16;
$tok16 = strtok("-");
$n16++;
}
$tok17 = strtok($dte16[3]," ");
$n17=1;
while ($tok17) {
$dte17[$n17]=$tok17;
$tok17 = strtok(" ");
$n17++;
}
$tok18 = strtok($dte17[2],":");
$n18=1;
while ($tok18) {
$dte18[$n18]=$tok18;
$tok18 = strtok(":");
$n18++;
}
echo $dte16[1].' '.$dte16[2].' '.$dte17[1].' '.$dte18[1].'
'.$dte18[2].' '.$dte18[3].'<p>';
echo $dte13[1].' '.$dte13[2].' '.$dte14[1].' '.$dte15[1].'
'.$dte15[2].' '.$dte15[3].'<p>';
if($dte18[3] >= $dte15[3])
{
$tot_sec = $dte18[3] - $dte15[3];
}
if($dte18[3] < $dte15[3])
{
$tot_sec = ($dte18[3] + 60) - $dte15[3];
$dte18[2] = $dte18[2] - 1;
}
if($dte18[2] >= $dte15[2])
{
$tot_min = $dte18[2] - $dte15[2];
}
if($dte18[2] < $dte15[2])
{
$tot_min = ($dte18[2] + 60) - $dte15[2];
$dte18[1] = $dte18[1] - 1;
}
if($dte18[1] >= $dte15[1])
{
$tot_hr = $dte18[1] - $dte15[1];
}
if($dte18[1] < $dte15[1])
{
$tot_hr = ($dte18[1] + 24) - $dte15[1];
$dte17[1] = $dte17[1] - 1;
}
$leapyr = $dte16[1] % 4;
if($dte17[1] >= $dte14[1])
{
$tot_day = $dte17[1] - $dte14[1];
}
if($dte17[1] < $dte14[1] && $dte16[2]==1 || $dte16[2]==3 || $dte16[2]==5 || $dte16[2]==7
|| $dte16[2]==8 || $dte16[2]==10 || $dte16[2]==12)
{
$tot_day = ($dte17[1] + 31) - $dte14[1];
$dte16[2] = $dte16[2] - 1;
}
if($dte17[1] < $dte14[1] && $dte16[2]==4 || $dte16[2]==6 || $dte16[2]==9 ||
$dte16[2]==11)
{
$tot_day = ($dte17[1] + 30) - $dte14[1];
$dte16[2] = $dte16[2] - 1;
}
if($dte17[1] < $dte14[1] && $dte16[2]==2 && $leapyr == 0)
{
$tot_day = ($dte17[1] + 29) - $dte14[1];
$dte16[2] = $dte16[2] - 1;
}
if($dte17[1] < $dte14[1] && $dte16[2]==2 && $leapyr > 0)
{
$tot_day = ($dte17[1] + 28) - $dte14[1];
$dte16[2] = $dte16[2] - 1;
}
if ($tot_sec < 10) {$tot_sec = '0'.$tot_sec;}
if ($tot_min < 10) {$tot_min = '0'.$tot_min;}
if ($tot_hr < 10) {$tot_hr = '0'.$tot_hr;}
if ($tot_day < 10) {$tot_day = '0'.$tot_day;}
if ($tot_sec == 0) {$tot_sec = '00';}
if ($tot_min == 0) {$tot_min = '00';}
if ($tot_hr == 0) {$tot_hr = '00';}
if ($tot_day == 0) {$tot_day = '00';}
echo $tot_day.' '.$tot_hr.':'.$tot_min.':'.$tot_sec;
--
http://www.php.net/manual/en/function.date.php
http://master.php.net/manage/user-notes.php?action=edit+20922
http://master.php.net/manage/user-notes.php?action=delete+20922
http://master.php.net/manage/user-notes.php?action=reject+20922