tags:

views:

63

answers:

3

How do I convert "9/7/2009" into a timestamp, such as one from time()? Do I use strtotime()?

+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
+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
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
I'm pretty sure it's MM/DD/YY as the standard format - am I incorrect?
Jonathan Kushner
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