technobizzmo Posted April 19, 2011 Share Posted April 19, 2011 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. //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 More sharing options...
dcro2 Posted April 19, 2011 Share Posted April 19, 2011 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 https://forums.phpfreaks.com/topic/234148-date_diff-function-not-accurate/#findComment-1203427 Share on other sites More sharing options...
technobizzmo Posted April 19, 2011 Author Share Posted April 19, 2011 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. Thanks for your help! Link to comment https://forums.phpfreaks.com/topic/234148-date_diff-function-not-accurate/#findComment-1203435 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.