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
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();

Link to comment
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();

 

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.