Jump to content

date_diff function not accurate


technobizzmo

Recommended Posts

I am trying to calculate the date and time difference between two timestamps.  I am using this for an auction style website and I thought it was working

fine until I woke up this morning and it said that there was only 1 hr left in auction when I actually started a 24 hour auction at like 7pm last night. :wtf:

 

//trying to find date diff accurately

$now = mktime(date("g"), date("i"), date("s"), date("m")  , date("d"), date("Y"));
$end = $timestamp;
$start = new DateTime();
$start -> setTimestamp($end);

$current = new DateTime();
$current -> setTimestamp($now);

$interval = date_diff($current, $start, true);

$days = $interval ->format('%d');
$hours = $interval ->format('%h');
$minutes = $interval ->format('%i');
$seconds = $interval ->format('%s');

 

This is how I created the timestamp to store in the database for auction.

//Creates timestamp for when auction ends using auction
//length chosen.
$timestamp  = mktime(date("g"), date("i"), date("s"), date("m")  , date("d")+$auction, date("Y"));

 

Link to comment
https://forums.phpfreaks.com/topic/234148-date_diff-function-not-accurate/
Share on other sites

I think your problem is riiight here: date("g")

'g' returns a 12 hour-format hour. mktime() expects a 24-hour format hour, otherwise it would ask for AM/PM as well. Change it to a capital G.

 

Btw, you know you can get the current timestamp with time(), right?

$now = time();

I think your problem is riiight here: date("g")

'g' returns a 12 hour-format hour. mktime() expects a 24-hour format hour, otherwise it would ask for AM/PM as well. Change it to a capital G.

 

Btw, you know you can get the current timestamp with time(), right?

$now = time();

 

Awesome! Fixed. It was the "g".  I was probably aware of that but i seem to always end up taking some complicated route. :P

Thanks for your help!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.