Question:
php date strtotime function problem?
pkvasan
2007-06-25 07:15:01 UTC
Hi

I am using date & strtotime function in my codes

I had given input as "12-02-2006" in php ver 5.2.1 the output produced is 12-02-2006 == Sunday 12th of February 2006 12:00:00 AM

With the same input if i test in php ver 4.3.1.1 the output produced is 12-02-2006 == Saturday 29th of July 2017 12:00:00 AM

Can any one help me what is problem here is it my code is mentioned below

$str = '12-02-2006';


if (($timestamp = strtotime($str)) === false) {
echo "The string ($str) is in valid";
} else {
echo "$str == " . date('l dS \o\f F Y h:i:s A', $timestamp);
}
?>

Thanks & Regards

PKVASAN
Three answers:
anonymous
2007-06-25 07:34:20 UTC
The problem is related to a change in how PHP computes time from 4.x to 5.x



If you used the format yyyy-mm-dd you would get the result you expect in 4.x; e.g., 2006-02-12.



http://us.php.net/manual/en/function.strtotime.php
cavite_homes
2007-06-25 14:46:19 UTC
This is not a bug in PHP but there are certain rules that strtotime still follows. The date that you specified is not considered a valid date format.



If you can edit your date format, then you will have to change it into something like these:

$str = '12/02/2006'; // format #1

$str = '2006-12-02'; // format #2



If you can't then you can try this:



$str = "12-02-2006";

if (preg_match('%(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20[0-9]{2})%', $str, $regs)) {

$month = $regs[1];

$day = $regs[2];

$year = $regs[3];

echo "$str == " . date('l dS \o\f F Y h:i:s A', mktime(0,0,0,$month,$day,$year));

} else {

echo "invalid date format?";

}





Frederick

----

For house and lot in Cavite,

visit http://cavitehomes.phpmanila.com

For videos of house and lot for sale in Cavite

visit http://cavitehomes.blogspot.com

Tagalog PHP tutorials

visit http://www.phpmanila.com
Moron
2007-06-25 14:30:36 UTC
From the PHP Manual



( Go to http://www.php.net/docs.php to get the actual version.)

Copyright © 1997 - 2007 the PHP Documentation Group



strtotime Example 2. Checking for failure




$str = 'Not Good';



// previous to PHP 5.1.0 you would compare with -1, instead of false

if (($timestamp = strtotime($str)) === false) {

echo "The string ($str) is bogus";

} else {

echo "$str == " . date('l dS \o\f F Y h:i:s A', $timestamp);

}

?>



NOTE the line

----> // previous to PHP 5.1.0 you would compare with -1, instead of false



Try their recommendation.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...