How do I convert "9/7/2009" into a timestamp, such as one from time()
? Do I use strtotime()
?
views:
63answers:
3
+7
A:
Yes you can use strtotime()
for that
$time = strtotime('9/7/2009');
echo $time; // 1252278000
This will assume a format of mm/dd/yyyy so don't try it with UK-style dd/mm/yyyy dates.
To go the other way, use date()
$date = date('n/j/Y', $time);
echo $date; // 9/7/2009
Greg
2009-12-01 18:30:05
+1
A:
strtotime() is correct.
http://php.net/manual/en/function.strtotime.php
You can test to be sure by putting the returned timestamp back in the date() function.
<?php echo date("m-d-Y",strtotime("9/7/2009")); ?>
Ian
2009-12-01 18:36:18
A:
Use strptime
if you want to be more precise about the format to parse, and remember that when people in the rest of the world see "9/7/2009" they read 9th July 2009, not 7th September 2009.
therefromhere
2009-12-01 18:43:25
I'm pretty sure it's MM/DD/YY as the standard format - am I incorrect?
Jonathan Kushner
2009-12-01 18:46:09
Yeah, `MM/DD/YY` is the format `strtotime()` assumes by default for months - but I was talking about when people read a date, not PHP.
therefromhere
2009-12-01 20:01:13