Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

$startdate = new DateTime("2013-11-15");
$enddate = new DateTime("2013-11-20");
$timestamp_start = strtotime($startdate);
$timestamp_end = strtotime($enddate);
$difference = abs($timestamp_end - $timestamp_start); 
$days = floor($difference/(60*60*24));
echo " ";
echo 'Days '.$days;
$months = floor($difference/(60*60*24*30));
echo 'Months '.$months;
$years = floor($difference/(60*60*24*365));
echo 'Years '.$years ;
echo " ";

before you answer, let me clariofy that my hosting provider doesnt support the version of php higher than 5.2, so dont suggest diff and interval functions.

i am getting Warning: strtotime() expects parameter 1 to be string, please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
600 views
Welcome To Ask or Share your Answers For Others

1 Answer

$startdate and $enddate are DateTime objects, not strings. strtotime() requires a string, and you should simply pass a string instead, like so:

$startdate = "2013-11-15";
$enddate = "2013-11-20";

I'd recommend ugprading to a higher PHP version, if possible. DateTime class is the best way to go when you're dealing with times and dates in PHP.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...