note 72384 modified in ref.datetime by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 02:21:42 +0000 |
| Subject: | note 72384 modified in ref.datetime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-141954@lists.php.net to get a copy of this message | ||
When debugging code that stores date/time values in a database, you may find yourself wanting to
know the date/time that corresponds to a given unix timestamp, or the timestamp for a given date
& time.
The following script will do the conversion either way. If you give it a numeric timestamp, it will
display the corresponding date and time. If you give it a date and time (in almost any standard
format), it will display the timestamp.
All conversions are done for your locale/time zone.
<?php
while (true)
{
// Read a line from standard in.
echo "enter time to convert: ";
$inline = fgets(STDIN);
$inline = trim($inline);
if ($inline == "" || $inline == ".")
break;
// See if the line is a date.
$pos = strpos($inline, "/");
if ($pos === false) {
// not a date, should be an integer.
$date = date("m/d/Y G:i:s", $inline);
echo "int2date: $inline -> $date\n";
} else {
$itime = strtotime($inline);
echo "date2int: $inline -> $itime\n";
}
}
?>
--was--
When debugging code that stores date/time values in a database, you may find yourself wanting to
know the date/time that corresponds to a given unix timestamp, or the timestamp for a given date
& time.
The following script will do the conversion either way. If you give it a numeric timestamp, it will
display the corresponding date and time. If you give it a date and time (in almost any standard
format), it will display the timestamp.
All conversions are done for your locale/time zone.
<?
while (true)
{
// Read a line from standard in.
echo "enter time to convert: ";
$inline = fgets(STDIN);
$inline = trim($inline);
if ($inline == "" || $inline == ".")
break;
// See if the line is a date.
$pos = strpos($inline, "/");
if ($pos === false) {
// not a date, should be an integer.
$date = date("m/d/Y G:i:s", $inline);
echo "int2date: $inline -> $date\n";
} else {
$itime = strtotime($inline);
echo "date2int: $inline -> $itime\n";
}
}
?>
http://php.net/manual/en/ref.datetime.php