note 85321 modified in function.checkdate by ross
| From: | ross@php.net | Date: | Sat, 01 Nov 2008 19:55:20 +0000 |
| Subject: | note 85321 modified in function.checkdate by ross | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146021@lists.php.net to get a copy of this message | ||
I wrote a simple function to converter datetime to UNIX timestamp. If the input time with error
format, the function will return current timestamp.
<?php
function datetime2timestamp($datetime)
{
$datetime = str_replace('-', ' ', $datetime);
$datetime = str_replace('/', ' ', $datetime);
$datetime = str_replace(':', ' ', $datetime);
$array = explode(' ', $datetime);
$year = $array[0];
$month = $array[1];
$day = $array[2];
$array[3] ? $hour = $array[3] : $hour = '00';
$array[4] ? $minute = $array[4] : $minute = '00';
$array[5] ? $second = $array[5] : $second = '00';
if (preg_match("/^(\d{4}) (\d{2}) (\d{2}) ([01][0-9]|2[0-3]) ([0-5][0-9])
([0-5][0-9])$/", "$year $month $day $hour $minute $second", $matches)) {
if (checkdate($matches[2], $matches[3], $matches[1])) {
return mktime(intval($hour), intval($minute), intval($second), intval($month), intval($day),
intval($year));
} else {
return time();
}
} else {
return time();
}
}
?>
--was--
I wrote a simple function to converter datetime to UNIX timestamp. If the input time with error
format, the function will return current timestamp.
function datetime2timestamp($datetime)
{
$datetime = str_replace('-', ' ', $datetime);
$datetime = str_replace('/', ' ', $datetime);
$datetime = str_replace(':', ' ', $datetime);
$array = explode(' ', $datetime);
$year = $array[0];
$month = $array[1];
$day = $array[2];
$array[3] ? $hour = $array[3] : $hour = '00';
$array[4] ? $minute = $array[4] : $minute = '00';
$array[5] ? $second = $array[5] : $second = '00';
if (preg_match("/^(\d{4}) (\d{2}) (\d{2}) ([01][0-9]|2[0-3]) ([0-5][0-9])
([0-5][0-9])$/", "$year $month $day $hour $minute $second", $matches)) {
if (checkdate($matches[2], $matches[3], $matches[1])) {
return mktime(intval($hour), intval($minute), intval($second), intval($month), intval($day),
intval($year));
} else {
return time();
}
} else {
return time();
}
}
http://php.net/manual/en/function.checkdate.php