1

I have a following string:

$time = "01/01/2015"; // format is m/d/Y

I want to convert it to an integer value using strtotime() function so that I can compare it with another date. This is the integer value that I get after I convert it to string:

strtotime($time); // Gives me => int(1420092000)

My Question:

Looking at the above format of time string, how will the strtotime() function know that the format of the string is "m/d/Y" or "d/m/y"? Do I have to specify the format in strtotime() function as a second parameter if that is possible?

1

2 Answers 2

3

As said on the php.net manual for strtotime:

Note:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

So, if you use slashes to seperate, it will assume month / day / year, and if you use dash or a dot, php will assume day - month - year.

http://php.net/manual/en/function.strtotime.php

Sign up to request clarification or add additional context in comments.

Comments

0

No you don't have to specify the format for strtotime(), you specify it with using /, . or -.

As a quote from the manual:

Note: Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

But if you want to see the implementation of this function you can search for it in the php github source code here: https://github.com/php/php-src

Here is the implementation of strtotime(): https://github.com/php/php-src/blob/master/ext/date/php_date.c#L1442

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.