1

i'm saving login time for php . this is what i use to save value against every login

$update= mysqli_query($connection, "UPDATE employees set loggedin= now() WHERE email = '$email'");       

when i get values from

  $logintime_query=  mysqli_fetc h_assoc($logintime);
  $loggedin_time_converted= explode(" ",$logintime_query) ;       

  $converted_time= $loggedin_time_converted['0'];
  $converted_date= $loggedin_time_converted['1'];

now i've to compare this date with today's date , to get today's date i am using this

$today=  $_SERVER['REQUEST_TIME'];
echo(date("Y-m-d",$today));

now problem is that $_SERVER['REQUEST_TIME] is giving me this date

2015-12-21

and when i enter date using NOW() and retrieve it from DB this is what i get

2015-12-20

is it time zone issue or what ? my time zone is utc+4 (Dubai) so i tried to set it like this

date_default_timezone_set('Asia/Dubai');

but this does not work some one please help me to figure this out

Sikander
  • 2,799
  • 12
  • 48
  • 100
  • 1
    database and php code is on same server ? Please make sure it – Mohammed Safeer Dec 20 '15 at 18:02
  • Sorry, but your PHP code you claim to be using just wont work. Can you check it, `mysqli_fetch_assoc()` returns an array and you are trying to explode it like it was a string. Also when you explode a datetime field like `2015-12-20 10:11:12` the date will be in occurance `[0]` and the time in occurance `[1]` – RiggsFolly Dec 20 '15 at 18:07

3 Answers3

1

Timezones are complicated and messy. For example, here your problem is not only concerning the PHP timezone setting, but your MySQL timezone as well since you're storing timestamps there and trying to use them again in PHP.

Enter Unix Timestamps for Transport

The better and more effective way to transport all these timestamps around so that you don't have to worry about timezone conversions when you go from MySQL to PHP or PHP to MySQL or PHP to Javascript or Javascript to PHP, is to just use Unix timestamps everywhere the time needs to be transported.

$logintime =  mysqli_query("SELECT UNIX_TIMESTAMP(loggedin) as loggedin FROM employees WHERE email = 'josh@foobar.baz'");

Now when you want to compare this in PHP you simply do ...

if ($logintime['loggedin'] > time()) {
    /* ... do stuff ... */
}

To go from PHP back to MySQL is just as simple...

$t = time();
$logintime =  mysqli_query("UPDATE employees SET loggedin = FROM_UNIXTIMESTAMP($t) WHERE email = 'josh@foobar.baz'");

This is so much more convenient than having to worry about whether or not you've correctly set timezones in various places and hope that you haven't made a simple mistake somewhere along the way that will mess with your results later.

Community
  • 1
  • 1
Sherif
  • 11,786
  • 3
  • 32
  • 57
0

I suggest you to use time() function only. See here.

If you set date_default_timezone_set('Asia/Dubai'); before using time() It should work.

not tested.

Community
  • 1
  • 1
LetsSeo
  • 875
  • 7
  • 20
0

It should be a timezone issue, yes. Whats the timezone of your mysql server?

You can globally change it with

default_time_zone='+04:00'

in the mysqld section of your mysqld.

Or with the following sql statement:

SET @@global.time_zone='+04:00';

Also be aware that $_SERVER['REQUEST_TIME] is the time the webserver started to process your request. Using php's date() function should be more accurate.

Dennis Stücken
  • 1,296
  • 9
  • 10