note 47059 added to function.mktime
| From: | lk at lkarisny dot com | Date: | Mon, 01 Nov 2004 16:05:22 +0000 |
| Subject: | note 47059 added to function.mktime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-79460@lists.php.net to get a copy of this message | ||
I needed to convert data with a DoY (Day of Year) and Year timestamp into a SQL standard timestamp
to do indexed searches on data. Here's my version of MySQL's MAKEDATE function:
<?php
class Time
{
private function calc_days_in_year($year)
{
return ($year & 3) == 0 && ($year%100 || ($year%400 == 0 && $year)) ?
366 : 365;
}
private function calc_daynr($year, $month, $day)
{
if( $year < 200 )
{
if(( $year = $year + 1900 ) < 1970 )
$year+=100;
}
$delsum = (bcmul(365,$year) + bcmul(31,($month-1)) + $day);
if($month <= 2)
$year--;
else
$delsum-=bcdiv((bcmul($month,4) + 23),10);
$temp = bcdiv(bcmul(bcadd(bcdiv($year,100),1),3),4);
return $delsum + bcdiv($year,4) - $temp;
}
private function get_date_from_daynr($daynr)
{
$days_in_month = array(31,28,31,30,31,30,31,31,30,31,30,31);
$year = bcdiv(bcmul($daynr,100),36525);
$temp = bcdiv(bcmul(bcadd(bcdiv($year,100),1),3),4);
$day_of_year = ($daynr - bcmul($year, 365)) - bcdiv(($year),4) + $temp;
while($day_of_year > ($days_in_year = $this->calc_days_in_year($year)))
{
$day_of_year-=$days_in_year;
$year++;
}
$leap_day = 0;
if($days_in_year == 366)
{
if($day_of_year > 31+28)
{
$day_of_year--;
if($day_of_year == 31+28)
$leap_day = 1;
}
}
$ret_month = 1;
$i = 0;
$day_of_year++;
for($ret_month = 1, $i = 0 ;
$day_of_year > $days_in_month[$i] ;
$ret_month++, $i++)
{
$day_of_year -= $days_in_month[$i];
}
$ret_year = $year;
$ret_day = $day_of_year - $leap_day;
$ret_month = str_pad($ret_month, 2, "0", STR_PAD_LEFT);
$ret_day = str_pad($ret_day, 2, "0", STR_PAD_LEFT);
return "$ret_year-$ret_month-$ret_day";
}
public function makedate($yearnr, $daynr)
{
$days = $this->calc_daynr($yearnr, 1, 1) + $daynr - 1;
if ($days >= 0 && $days < 3652424)
{
$str = $this->get_date_from_daynr($days);
return $str;
}
return 0;
}
}
$time = new Time();
// Returns timestamp as YYYY-MM-DD
$timestamp = $time->makedate(2004, 306);
echo $timestamp
?>
This could probably be organized better and could use some error checking, but thats the general
functionality of it. It requires bcmath for binary precision calculations.
----
Manual Page -- http://www.php.net/manual/en/function.mktime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47059
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47059&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47059&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47059&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47059&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47059&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47059&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47059&report=yes
Search -- http://master.php.net/manage/user-notes.php