Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Figure out the duration between 2 time columns in mysql table

Posted on 2011-10-10
10
Medium Priority
?
270 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:tjyoung
  • 5
  • 3
  • 2
10 Comments
 
LVL 7

Accepted Solution

by:
ziceva earned 2000 total points
ID: 36944134

$time_in_seconds=strtotime('2011-10-08 20:22:37')-strtotime('2011-10-08 19:56:35');

http://www.php.net/manual/en/function.strtotime.php
0
 
LVL 7

Expert Comment

by:amigura
ID: 36944278
$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

0
 
LVL 1

Author Comment

by:tjyoung
ID: 36944554
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?
0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
LVL 7

Expert Comment

by:ziceva
ID: 36944563
Have you tried the first solution? What was the problem with it?
0
 
LVL 1

Author Comment

by:tjyoung
ID: 36944570
running your exact code as a standalone also creates the same error.
0
 
LVL 1

Author Comment

by:tjyoung
ID: 36944584
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.
0
 
LVL 1

Author Comment

by:tjyoung
ID: 36944588
Wait a minute, I didn't even notice the code.. duh.
checking it now. I just saw the link.
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 36944601
Works perfect.
thanks!
0
 
LVL 7

Expert Comment

by:amigura
ID: 36944643
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

0
 
LVL 7

Expert Comment

by:amigura
ID: 36944684
ok maybe not lol
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question