note 32268 added to function.strtotime
| From: | tippy2k at rack1 dot php dot net | Date: | Thu, 22 May 2003 00:29:06 +0000 |
| Subject: | note 32268 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-48802@lists.php.net to get a copy of this message | ||
For US date formating (mm-dd-yy[yy]) to Unix Timestamp try this example:
<html>
<body>
<?
function stripdash($date) {
for($i=1; $i < 10; $i++) {
$find = "-0".$i."-";
$replace = "/".$i."/";
$date = str_replace($find, $replace, $date);
}
for($i=1; $i < 32; $i++) {
$find = "-".$i."-";
$replace= "/".$i."/";
$date = str_replace($find, $replace, $date);
}
return $date;
}
if ($field) {
$date = trim($field);
$date = stripdash($date);
echo "Date Submited: $date<br>"; // with stripdash
$timestamp = strtotime ($date);
$new_date = getdate ($timestamp);
echo "Date Computed: ".$new_date['month']."
".$new_date['mday'].",
".$new_date['year']."<br>";
echo "Timestamp Computed: $timestamp<br>";
}
?>
<form name="form1" method="post" action="">
Date:<br>
<input name="field" type="text">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
This script will display -1 if it encounters a date out of range (< 1903 or > 2038) or in some
cases where century (yyyy) is needed to calculate the timestamp.
Thanks to irmak for the idea.
----
Manual Page -- http://www.php.net/manual/en/function.strtotime.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+32268
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+32268&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+32268&report=yes