PHP: strtotime returns "nothing" of type "boolean"
Asked Answered
F

2

5

I have the variable $EDate, I used strtotime function with this variable with different values and the result was as following:

$EDate = 10-21-2013;    echo "strtotime($EDate)";   the result = nothing    and the type is boolean
$EDate = 09-02-2013;    echo "strtotime($EDate)";   the result = 1360386000 and the type is integer
$EDate = 09-30-2013;    echo "strtotime($EDate)";   the result = nothing    and the type is boolean
$EDate = 09-30-2013;    echo "strtotime($EDate)";   the result = nothing    and the type is boolean
$EDate = 07-02-2014;    echo "strtotime($EDate)";   the result = 1391749200 and the type is integer
$EDate = 10-12-2014;    echo "strtotime($EDate)";   the result = 1418187600 and the type is integer

Can anybody explain this and how to avoid the boolean result?

Filamentous answered 3/10, 2013 at 16:56 Comment(0)
L
4

From the documentation:

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.

Your code assumes that the date is in d-m-y format, and will return FALSE since the month values are incorrect:

var_dump(strtotime('10-21-2013')); // no month 21
var_dump(strtotime('09-30-2013'));
var_dump(strtotime('09-30-2013'));

If you want to be able to use custom formats, use DateTime::createFromFormat() instead:

$date = DateTime::createFromFormat('m-d-Y', '10-21-2013');
echo $date->format('U');

Demo!

Legendary answered 3/10, 2013 at 17:3 Comment(10)
I used var_dump and the results 09-02-2013 ==> int(1360386000) 09-02-2013 ==> string(10) "09-02-2013" 10-21-2013 ==> string(10) "10-21-2013" 09-02-2013 ==> string(10) "09-02-2013" 09-30-2013 ==> string(10) "09-30-2013" 09-30-2013 ==> string(10) "09-30-2013" 07-02-2014 ==> string(10) "07-02-2014" 10-12-2014 ==> string(10) "10-12-2014"Filamentous
@Mayada: That's expected. Your code assumes that the format is d-m-y (because of the separator used). The solution would be to use DateTime::createFromFormat(). See the demo I linked above.Legendary
I think the var_dump help me to do the comparison of two dates.Filamentous
@Mayada: Nope, that's not what var_dump does. It's used to know the type and value (and more) of an (or more) expressions. See the documentation for var_dump.Legendary
No it doesn't. I will try DateTime::createFromFormat()Filamentous
@Mayada: Sure. Does the above code produce the output you wanted?Legendary
The problem is, I have date $Today which equal (10/03/2013) it is string. I need to compare the values of dates in database field which stored as a string (m-d-y) with $Today I tried many things like strtotime and format date but every hay has a problem.Filamentous
@Mayada: That's exactly what the above code does. It will work for all m-d-Y dates. Can you give me an example date that doesn't work?Legendary
Yeeeeeeeee, it is solved by using $date = DateTime::createFromFormat('m-d-Y', '10-21-2013'); echo $date->format('U'); many many thanks Amal you saved my day, I spent more than 3 days to solve this problem> شكرا جزيلا Also I would thank ComFreek for answering my quesion.Filamentous
@Mayada: Glad to have been of help! :)Legendary
B
3

Edit: This answer does not apply anymore to the question, see my comment below.

Put your values in quotes, so they become a string:

$EDate = '10-21-2013'; 
...

Your current code does a mathematical subtraction: 10 - 12 - 2013 = -2015.

Boule answered 3/10, 2013 at 16:58 Comment(4)
I grabbed this values from string database field, so the variable is string.Filamentous
@Mayada Can you execute var_dump($EDate), please? (I've just seen Anmal Murali's answer. This should be the actual reason.)Boule
@ComFreek: It's Amal, btw. I think the OP forgot to add the quotes when he posted the code here -- but anyway, this answer is also correct. :)Legendary
@AmalMurali Oh I'm sorry that I mispelt your name. Yes, exactly. Happens quite often that OP don't post their actual code or introduce bugs in the posted code.Boule

© 2022 - 2024 — McMap. All rights reserved.