-1

Possible Duplicate:
strtotime date weird result

I am trying to compare two times for an attendance system, i am trying to get the accurate time for Arkansas,USA but im not being to able to get the correct time. Please, guide me in the right direction if i am doing something wrong, part of my code:

$curTime =DATE("H:i A", strtotime('GMT-6'));

foreach($res as $r)
{
    $crs_id =$r['course_id']; 
    $query1 = "select * from tbl_course_time where course_id='$crs_id'";

     $res1 = $obj->querySelect($query1);


     $start_time  = DATE("H:i", STRTOTIME($res1[0]['start_time']));
     $end_time = DATE("H:i", STRTOTIME($res1[0]['end_time']));

    if ($curTime > $start_time && $curTime < $end_time) 
    {

      $qry="insert into tbl_attendance set         stud_id='$id',attd_date='$curDateForDB',attd_time='$curTimeForDB',course_id='$attd_courseId'";

    $res=$obj->queryInsert($qry);

    $result1 = "taken";
   }
}

It's giving me incorrect time's is it the GMT time im getting wrong or something else Any help would be greatly appreciated, Thank you

Community
  • 1
  • 1
jarus
  • 1,853
  • 11
  • 44
  • 76

2 Answers2

1

Don't use strtotime('GMT-6') because GMT-6 is not a time; it's a time zone.

// gets the UTC time using gmdate() instead of date()
$utc_str = gmdate("M d Y H:i:s", time());

// get the UTC timestamp, should you need it
$utc = strtotime($utc_str);

If you're in PHP 5, the DateTime class is a very clean way to get times and also deal with time zone conversions.

Also, I encourage you to set your database up using UTC time, and convert the output to the user's time zone.

Anton
  • 3,998
  • 25
  • 40
  • i have tried using date time class when i try to do that, it just gives me incorrect time also, cud u please help with some code snippets, thanks – jarus Dec 13 '12 at 14:52
  • Does the above code give me time in seconds? – jarus Dec 13 '12 at 14:58
  • Yes, time is formatted in seconds starting at the UNIX epoch, 00:00:00 UTC on 1/1/1970. This is absolute, and time-zone free, so you can then convert to the local time. – Anton Dec 13 '12 at 14:59
0

You're doing the gmt-6 business wrong. The following is on a server that's in GMT-6 already:

echo date('r');                     // Thu, 13 Dec 2012 02:40:00 -0600
echo date('r', strtotime('gmt-6')); // Thu, 13 Dec 2012 08:40:17 -0600

Note the 6 hour difference (and 17 seconds while I was typing up the strtotime bits). Doing the gmt-6 bit in strtotime forces PHP to apply a 6 hour difference to a string that's already in some timezone already.

Marc B
  • 356,200
  • 43
  • 426
  • 500