note 93957 added to function.date
| From: | MarcinSzychowski at osu1 dot php dot net | Date: | Thu, 08 Oct 2009 13:59:35 +0000 |
| Subject: | note 93957 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161507@lists.php.net to get a copy of this message | ||
MS-DOS related filesystems, along with ZIP files, store date and time in four bytes (time: 2 bytes,
date: 2 bytes), as described in Wikipedia: http://en.wikipedia.org/wiki/File_Allocation_Table#Directory_table
As it took me some time to not-find any functions to convert unix to ms-dos timestamps, I decided to
put these simple functions here, to save somebody else's time:
<?php
// $ts - standard UNIX timestamp, as returned by mktime()
function packTimeDate($ts){
// MS-DOS can store dates ranging from 1980-01-01 up to 2107-12-31
$year=date('Y', $ts);
if(($year<1980) || ($year>2107)) return "\x00\x00";
else return packTime($ts).packDate($ts);
}
/** From Wikipedia:
* 15-11 Hours (0-23)
* 10-5 Minutes (0-59)
* 4-0 Seconds/2 (0-29)
**/
function packTime($ts){
$sec=round((('1'.date('s', $ts))-100)/2);
$min=('1'.date('i', $ts))-100;
$hour=date('G', $ts);
$dosTime=($hour<<11)+($min<<5)+$sec;
$m[0]=$dosTime%256;
$m[1]=(($dosTime-$m[0])/256)%256;
return sprintf('%c%c', $m[0], $m[1]);
}
/** From Wikipedia:
* 15-9 Year (0 = 1980, 127 = 2107)
* 8-5 Month (1 = January, 12 = December)
* 4-0 Day (1 - 31)
**/
function packDate($ts){
$year=date('Y', $ts)-1980;
$day=date('j', $ts);
$month=date('n', $ts);
$dosDate=($year<<9)+($month<<5)+$day;
$m[0]=$dosDate%256;
$m[1]=(($dosDate-$m[0])/256)%256;
return sprintf('%c%c', $m[0], $m[1]);
}
?>
As I didn't need to unpackTimeDate, I didn't bother to write reverse functions. Feel free
to provide yours if You care.
Have fun with PHP!
----
Server IP: 82.177.164.100
Probable Submitter: 217.113.233.38
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/93957
Del: integrated -- https://master.php.net/note/delete/93957/integrated
Del: useless -- https://master.php.net/note/delete/93957/useless
Del: bad code -- https://master.php.net/note/delete/93957/bad+code
Del: spam -- https://master.php.net/note/delete/93957/spam
Del: non-english -- https://master.php.net/note/delete/93957/non-english
Del: in docs -- https://master.php.net/note/delete/93957/in+docs
Del: other reasons-- https://master.php.net/note/delete/93957
Reject -- https://master.php.net/note/reject/93957
Search -- https://master.php.net/manage/user-notes.php