Link to home
Start Free TrialLog in
Avatar of tjyoung
tjyoung

asked on

Figure out the duration between 2 time columns in mysql table

Hi,
I have a table that contains two dates formatted like this:

I have a campaign_start_time: 2011-10-08 19:56:35
and a campaign_end_time: 2011-10-08 20:22:37

I'm trying to figure out how to get duration of time between the 2. Essentially my campaign_duration.

And how to extract just the dates without the times?

Any idea how to accomplish this?

thanks tj
ASKER CERTIFIED SOLUTION
Avatar of ziceva
ziceva
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
$sdate = new DateTime('2011-10-08 15:56:35'); // campaign_start_time
$edate = new DateTime('2011-10-09 20:22:37');  // campaign_end_time

$no_duration = $edate->diff($sdate);

echo $no_duration->format('%d days, %h hours, %i minutes')."\n";

Open in new window

Avatar of tjyoung
tjyoung

ASKER

Trying to work it out but am getting this error:

Call to a member function diff() on a non-object

Bascially doing this with the code since my start and end dates are set:

$interval = $campaignstart->diff($campaignend);
$interval->format('%d days, %h hours, %i minutes');

See anything obvious?
Have you tried the first solution? What was the problem with it?
Avatar of tjyoung

ASKER

running your exact code as a standalone also creates the same error.
Avatar of tjyoung

ASKER

Hi,
The first solution was more of a direction to 'go and learn all about this' as opposed to 'here is the relevant code that solves your issue'. Which is why I tried the second one. Unfortunately that doesn't seem to work for some reason.

I'll take another look at the link.
Avatar of tjyoung

ASKER

Wait a minute, I didn't even notice the code.. duh.
checking it now. I just saw the link.
Avatar of tjyoung

ASKER

Works perfect.
thanks!
ziceva: i think he wants more than just seconds.

your code should be, if your missing first 2 lines

$sdate = new DateTime($campaignstart);
$edate = new DateTime($campaignend);

$interval = $edate->diff($sdate);

echo $interval->format('%d days, %h hours, %i minutes')."\n";

Open in new window

ok maybe not lol