graham23s
08-27-2007, 02:35 PM
Hi Guys,
in mysql i have 2 files (date files)
they are:
1)2007-08-27 13:20:03
2)August 13, 2007, 10:02 pm
they are displayed differently, is there a way i can work out the days elapsed between them like this? or do the time stamps need to be the same?
thanks for any help guys
Graham
mlseim
08-27-2007, 03:24 PM
They need to be the same format, but it's easy to do.
The first date is easy: 2007-08-27 13:20:03 (format is - )
The next one is a bit harder: August 13, 2007, 10:02 pm
See this site as an example:
http://www.developertutorials.com/tutorials/php/calculating-difference-between-dates-php-051018/page1.html
I'm wondering if MySQL can convert too.
graham23s
08-27-2007, 04:02 PM
Hi Mate,
i have since made them both the same format for easiness (i should have done that in the first place i guess lol)
they are now:
2007-08-27 15:22:18
2007-08-13 22:02:28
i just need to work out the days elapsed but not sure how any help would be appreciated
thanks mate
Graham
Inigoesdr
08-27-2007, 04:11 PM
$d1 = strtotime('2007-08-27 15:22:18');
$d2 = strtotime('2007-08-13 22:02:28');
$days = ceil(($d1 - $d2) / (60 * 60 * 24));
echo $days; // 14
If this is in MySQL it has date functions that are very easy to use to compare dates and count days.
graham23s
08-27-2007, 05:46 PM
i'm stumped lol now code is:
$time_now = time();
$d2 = date('Y-m-d H:i:s', $time_now);
$d1 = date('Y-m-d H:i:s', strtotime($ng_date));
$days = ceil(($d1 - $d2) / (60 * 60 * 24));
when echoing out it produces:
2007-06-02 16:53:24
2007-08-27 18:00:51
which is right, but $days echoes out as 0.
can anyone help lol
cheers
Graham
Inigoesdr
08-27-2007, 09:14 PM
$days uses timestamps. strtotime() generates them from the date/time string.
If you're going to use those date formats then you need something like this:
$days = ceil((strtotime($d1) - strtotime($d2)) / (60 * 60 * 24));
graham23s
08-27-2007, 11:16 PM
thanks mate thats perfect.
Graham