In PHP, does date() take server or user time?

Asked

Viewed 1,722 times

3

I have a question about date, it takes the time from the server where my site is located or the machine the user is accessing?

I’m taking a look at the PHP Manual but I couldn’t figure out which local method it refers to: machine or user.

  • 1

    According to the page PHP 5 Date and Time: PHP date() Function will Return the Current date/time of the server!.

3 answers

4

I’m having a question about the date, it takes the time from the server where the my site is located or the machine the user is accessing?

The class or function Datetime/date gets the information (date or time) of the server php is installed and not from the user’s machine.

The time can be set by Timezone which is the time zone of a country or region, Brasilia time is the (GMT/UTC -3) three hours late compared to greenwitch time.

Setting the time zone is a good idea because avoid the following error:

It is not safe to rely on the system’s Timezone Settings. Please use the date.Timezone Setting, the TZ Environment variable or the date_default_timezone_set() Function.

This can be done in two ways the first is to assign a valid Timezone in php.ini that setting will be valid for the entire server. The bottom line should be modified (after the change restarts the server):

date.timezone = America/Sao_Paulo

The second method is to use the function date_default_timezone_set() once called it applies only to the script.

date_default_timezone_set('America/Sao_Paulo');

Example - ideon

List of valid Zones teams

  • If I use ( date_default_timezone_set ("America/Sao_paulo"); ) you can define what time I want the date to look for ?

  • 2

    This sets the clock that the server will work which in case is 3 hours late compared to greenwitch

4


It refers to the server date. To adjust to a specific and correct date, use the function date_default_timezone_set, an example of working with São Paulo time

<?php
date_default_timezone_set('America/Sao_Paulo');
?>

To see the list of timezones see at PHP documentation

ps: always put this line at the beginning of your PHP code

  • Yes, I always use, but it was a doubt that had to take out of the head, rs.

  • I was getting confused with javascript...

  • 1

    rsrs, it happens. If you took it from the user it would be confusing when you use the date to be recorded in your database. For example, you want to know what time the user logged into the system, in your bank would be 13h30, but it is still 10h15 in the morning, so would confuse everything.

  • I’m recording to analyze the entry of users on the site tired of google Analytic... would be a total madness...

  • 1

    So always adjust the time with the function quoted. At least you will have a more specific control of the times.

3

You can use the timezone so he can get the time you want:

setlocale(LC_ALL, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
date_default_timezone_set('America/Sao_Paulo');

It is also possible to define more specifically:

// set default timezone
date_default_timezone_set('America/Sao_Paulo'); // CDT

$info = getdate();
$date = $info['mday'];
$month = $info['mon'];
$year = $info['year'];
$hour = $info['hours'];
$min = $info['minutes'];
$sec = $info['seconds'];

$current_date = "$date/$month/$year == $hour:$min:$sec";

See more in this reply by Soen.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.