0

I have this line of code:

<?= date('F dS, Y',$user_info['date_of_birth']) ?>

It gets the unix birtday date per user and converts it in a normal date. The only problem is that it will show one day earlier. For example, the unix date in the database is 11th may 1990. In the view it will output, 10th may 1990. Do you know how to fix this issue?

Thanks in advance

mr john
  • 5
  • 6

3 Answers3

0

Check into synchronizing your MySQL and PHP timezones. Check out this similar question/answer:

Set timezone in PHP and MySQL

Community
  • 1
  • 1
Nick
  • 16,066
  • 3
  • 16
  • 32
  • Thanks! for the info:) But do you know how to add +1 day in the line of code above? – mr john Jul 05 '16 at 02:51
  • Try something like this: date('F dS, Y', strtotime($user_info['date_of_birth'] . ' +1 day')) . – Nick Jul 05 '16 at 02:53
  • I do agree with chris, you should probably just take the steps to align timezones so you don't run into similar issues elsewhere in the application. – Nick Jul 05 '16 at 02:57
  • When I tried your code I got an output of December 31st, 1969 and it suppose to be 11th may 1993 – mr john Jul 05 '16 at 03:48
0

To explicitly answer your question, you can do the following:

<?=date('F dS, Y',($user_info['date_of_birth'] + 86400))?>

Where 86400 is the number of seconds in a day.


However, as others suggested, you can and should sync up the database and Php to UTC/GMT time.

For Php timezone, do the following at top of your scripts:

date_default_timezone_set('UTC');

For SQL, that depends on the database you're using (I don't believe you specified the database you're using).

skidadon
  • 547
  • 4
  • 7
0

It is probably time on you server is wrong. Is you have accesse to it shell you can check it by date command:

[centos@application ~]$ date
Tue Jul  5 04:17:00 UTC 2016

if not then you probably will have to do like this :

define('TIMEZONE_OFFSET', 86400);
<?=date('F dS, Y',($user_info['date_of_birth'] + TIMEZONE_OFFSET))?>

Again if you have access to mysql console check its timezone settings.

set-timezone-in-php-and-mysql

Also set manually php defaulte timezone:

date_default_timezone_set('UTC');
Community
  • 1
  • 1
Maksym Semenykhin
  • 1,924
  • 15
  • 26