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
430 views
in Technique[技术] by (71.8m points)

php - Time diff in minutes between 2 dates

Got this working on php 5.3

$datetime1 = new DateTime("2011-10-10 10:00:00");
$datetime2 = new DateTime("2011-10-10 10:45:00");
$interval = $datetime1->diff($datetime2);
$hours   = $interval->format('%h'); 
$minutes = $interval->format('%i');
echo 'Diff. in minutes is: '.($hours * 60 + $minutes); 

How can i make it work on php 5.2 ? are there any equivalent functions available??

Got it working

$date1 = "2011-10-10 10:00:00";
$date2 = "2011-10-10 10:11:00";
echo round((strtotime($date2) - strtotime($date1)) /60);
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Instead of DateTime you can use strtotime and date:

$datetime1 = strtotime("2011-10-10 10:00:00");
$datetime2 = strtotime("2011-10-10 10:45:00");
$interval  = abs($datetime2 - $datetime1);
$minutes   = round($interval / 60);
echo 'Diff. in minutes is: '.$minutes; 

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

...