note 93957 deleted from function.date by tularis
| From: | tularis@php.net | Date: | Thu, 09 Feb 2012 20:55:23 +0000 |
| Subject: | note 93957 deleted from function.date by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185979@lists.php.net to get a copy of this message | ||
Note Submitter: Marcin Szychowski
----
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!