Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php datetime doesn't match with my windows system

Tags:

php

I used date("Y-m-d H:i:s"); to get current date time format but the result doesn't match with my Windows system.

Example:

<?php
echo "Current date time: ".date("Y-m-d H:i:s")."<br />";
?>

The sample code displaying result:

Current date time: 2012-05-30 01:58:21

But my Windows system date time is 2012-05-30 09:56:04

I have tried using date_default_timezone_set('Asia/Singapore') function to get my region datetime and it's work but I want it set default in my php.ini.

For example: (it's work)

<?php
date_default_timezone_set('Asia/Singapore');
echo "Current date time: ".date("Y-m-d H:i:s")."<br />";
?>

I have also tried to set default value in php.ini that is date.timezone = "" changed to date.timezone = "Asia/Singapore" but doesn't work.

I use Windows 7 Home Premium 64bit and php-5.3.13-Win32-VC9-x86. Does anyone know how to match between these value?

like image 593
Manellen Avatar asked May 30 '12 02:05

Manellen


1 Answers

date_default_timezone_set will supersede php default timezone setting, without needing to restart anything. But in your case it did not work. that's weird. Try this as this is my last resort:

<?php
header('Content-Type: text/plain');
//date_default_timezone_set('Asia/Singapore');
date_default_timezone_set('Etc/GMT+8');
echo date("Y-m-d H:i:s A T", time());
?>

UPDATES:

Since what you need is a default php timezone and permanent solution without using date_default_timezone_set, following the few simple steps below:

  1. Create a file called phpinfo.php with one line content <?php echo phpinfo(); ?>
  2. Run http:// localhost /phpinfo.php and look for Loaded Configuration File. Example value C:\wamp\bin\apache\Apache2.2\bin\php.ini
  3. Open the above php.ini file with a notepad. Look for the [Date] and modify as below:

    [Date]
    ; Defines the default timezone used by the date functions
    ; http://php.net/date.timezone
    date.timezone = Asia/Singapore
    
  4. Save it and restart your apache. Run again your datetime.php. Or you can also verify directly from phpinfo.php under a h1 header date by making sure the default timezone is read as "Asia/Singapore" like below: enter image description here
like image 115
You Qi Avatar answered Oct 11 '22 10:10

You Qi