check date difference with php

Go To StackoverFlow.com

-1

Possible Duplicate:
How to calculate the difference between two dates using PHP?

how can i check in php, the date difference with the following given php variables?

$year = 2012;
$month = 5;
$day = 20;
$hour = 23;
$min = 15;
$sec = 0;

what i'm looking to do

i want to compare the php variables shown above with actual current time, and then output the difference between the current time and the php varables, in days or even hours? which is the best and fastest way in going about doing something like this in php?

2012-04-04 23:37
by codrgi


2

mktime

$time_diff = mktime($hour, $min, $sec, $month, $day, $year) - time();

Will give you the difference in seconds. You can use that to display it however you want.

2012-04-04 23:42
by Paulpro


2

Build a date string from those variables. Use the DateTime Object and use its functions to do what you would like such as $dateTime->diff(new DateTime("now")) and read up on DateInterval to see how to get back what'd you like.

EDIT: This assumes you have PHP 5.3.

2012-04-04 23:42
by Norm


1

Use mktime to convert into UTC and then use simple maths to calculate differences.

2012-04-04 23:46
by DaveyBoy
Ads