Conversion
|
|
|
|
<?php /**
* my_date
* Dates are a bit of a mess everywhere. This class converts and outputs dates
* from one format to another.
*
* Accepted input_type names: unix - unix format date
* mysql_date - mysql date format (yyyy-mm-dd hh:mm:ss etc)
* mysql_timestamp - mysql timestamp format (yyyymmdd and yyyymmddhhmmss - two digit year versions throw an error)
* array (associative with keys year, mon, mday, hours, minutes, seconds)
* rfc822 - RFC 822 with four digit year
* Access dates as follows:
* $myDate->mysql_date - gives yyyy-mm-dd style
* $myDate->mysql_datetime - gives yyyy-mm-dd hh:mm:ss style
* $myDate->mysql_timestamp - gives yyyymmddhhmmss style
* $myDate->unix - gives unix timestamp
* $myDate->array - returns getdate style array
* $myDate->dublin - dublin core compatible date time
* $mayDate->rfc822 - human readable date, and format for RSS
* @author dunx
* @copyright Copyright Duncan Drury (c) 2004
**/ class myDate {
var $input; # what was input as the date
var $input_type; # the input type
var $mysql_date; # mysql version of the date
var $mysql_datetime;# mysq date with time
var $mysql_timestamp;#full mysql timestamp (14)
var $unix; # unix timestamp date
var $array; # php array from getdate
var $rfc822; # rfc 822 format date
function myDate($input = 'NOW', $input_type = 'unix') {
$this->input = $input;
$this->input_type = $input_type;
# if no input sent, set up input as current day and time Unix timestamp
if ($input =="NOW") $input = time();
# use appropriate function for time
switch($input_type){
case 'unix':
$this->unix = $input;
break;
case 'mysql_date':
$this->unix = $this->mysqlDate2unix($input);
break;
case 'mysql_timestamp':
$this->unix = $this->mysqlTimestamp2unix($input);
break;
case 'array':
$this->unix = $this->array2unix($input);
} // switch
# Now create all the dates from the unix version
$this->createAll();
}
function mysqlDate2unix($input){
#string is in format YYYY-MM-DD, or YYYY-MM-DD HH:MM:SS
return strtotime($input);
}
function mysqlTimestamp2unix($input){
# string is in mysql timestamp format YYYYMMDD etc
# determine which timestamp format is in use by looking at length of string
$length = strlen($input);
if ($length == 12 || $length == 6) trigger_error('Please do not send two digit year timestamps to myDate object!',E_USER_ERROR);
# first 4 digits are year, next 2 are month, next 2 are day
$y = substr($input,0,4);$m = substr($input,4,2);$d = substr($input,6,2);
if ($length == 8) {
return mktime(0,0,0,$m,$d,$y);
} else {
$h = substr($input,8,2);$min = substr($input,10,2);$s = substr($input,12,2);
return mktime($h,$min,$s,$m,$d,$y);
}
}
function array2unix($input){
# check if an array has been sent
if (is_array($input) == false) trigger_error('You said you were sending an array to myDate object, but sent something else.',E_USER_ERROR);
return mktime($input['hours'],$input['minutes'],$input['seconds'],$input['mon'],$input['mday'],$input['year']);
}
function createAll(){
$this->mysql_date = date('Y-m-d',$this->unix);
$this->mysql_datetime = date('Y-m-d H:i:s',$this->unix);
$this->mysql_timestamp = date('Ymdhis',$this->unix);
$this->array = getDate($this->unix);
$this->rfc822 = date('r',$this->unix);
}
function debugDateOutput(){
echo '<div style="border:red thick solid;padding:5px;">';
echo "myDate debug<br/>";
echo "<strong>Input:</strong> $this->input <strong>Type:</strong> $this->input_type<br/>";
echo "<strong>Unix:</strong> $this->unix<br/>";
echo "<strong>mySQL date:</strong> $this->mysql_date<br/>";
echo "<strong>mySQL datetime:</strong> $this->mysql_datetime<br/>";
echo "<strong>mySQL timestamp:</strong> $this->mysql_timestamp<br/>";
echo "<strong>array</strong> Day: {$this->array['mday']}, Month: {$this->array['mon']}, Year: {$this->array['year']}, Hour: {$this->array['hours']}, Mins: {$this->array['minutes']}, Secs: {$this->array['seconds']}<br/>";
echo "<strong>RFC 822:</strong> $this->rfc822</br/>";
echo '</div>';
}
} /* End of myDate class */ ?>
|
|
|
Usage Example
|
$theDate = new myDate('2004-03-25','mysql_date');
$theDate->debugDateOutput();
|
|
|
Rate This Script
|
|
|
|