0

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

4 Answers 4

7

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
Sign up to request clarification or add additional context in comments.

Comments

2

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")); ?>

Comments

1

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.

2 Comments

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

A newer way to do this as of PHP 5.3 is to use the DateTime class with the getTimestamp() method:

$datetime = new DateTime('9/7/2009');
echo $datetime->getTimestamp();

See it in action

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.