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

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

php - Warning: strtotime() expects parameter 1 to be string

$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

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

1 Answer

0 votes
by (71.8m points)

$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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...