date_default_timezone_set doesn't work

Status
Not open for further replies.

springbox99

Member
Messages
114
Reaction score
3
Points
18
Hello,

Since php 5.4 is used by x10hosting, date_default_timezone_set('Europe/Paris') doesn't work.
My website returns server's hour instead of Paris' hour.

I've not found any documentation which says that the function is deprecated or something like that.
I don't know how to solve the problem.

Thank you.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
PHP:
<?php

echo date_default_timezone_get();
echo "\n<br />\n" . date("H:i:s", time());
date_default_timezone_set("America/Los_Angeles");
echo "\n<br />\n" . date("H:i:s", time());
date_default_timezone_set('Europe/Paris');
echo "\n<br />\n" . date("H:i:s", time());
?>

Gives me:

Code:
America/Chicago
09:09:29
07:09:29
16:09:29

So the function seems to be operational (at least on my server Vox -- you are on Level)

Could you try my script on your site and see what happens?
 

bdistler

Well-Known Member
Prime Account
Messages
3,534
Reaction score
196
Points
63
This code works for me on server [ Level ]...

PHP:
<?php
error_reporting(E_ALL);

$script_default_tz = 'Europe/Paris';

print "PHP version ==> " . phpversion() . "<br>\n";

print "Default time zone " . date_default_timezone_get() . "<br>\n";
print "Time ==> " . date("m/d/Y H:i:s",time()) . "<br>\n";

if (date_default_timezone_set($script_default_tz) == TRUE)
  {
    print $script_default_tz . " default time zone set <br>\n";
  }
  else
    {
      print $script_default_tz . " timezone identifier is not valid<br>\n";
    }

print "Default time zone " . date_default_timezone_get() . "<br>\n";
print "Time ==> " . date("m/d/Y H:i:s",time()) . "<br>\n";
?>

ouputs...

PHP version ==> 5.4.17
Default time zone America/New_York
Time ==> 09/09/2013 10:23:24
Europe/Paris default time zone set
Default time zone Europe/Paris
Time ==> 09/09/2013 16:23:24

You can set the time zone for all of your PHP scripts in your [ .htaccess ] file with this line
Code:
php_value date.timezone "Europe/Paris"
 
Last edited:

springbox99

Member
Messages
114
Reaction score
3
Points
18
Thank you for your answers.
I've tried bdistler's script. It works but it doesn't help.
It seems that the problem comes from the database and not from php, as i thought.
Any idea ?
Thank you again.
 

springbox99

Member
Messages
114
Reaction score
3
Points
18
Thanks

It seems to be something like that but "mysql_query(“SET time_zone = ‘-6:00′”)" gives :
Warning
: mysql_query(): Access denied for user '***'@'localhost' (using password: NO) in /home/***/public_html/minichat.php on line 11

Warning: mysql_query(): A link to the server could not be established in /home/***/public_html/minichat.php on line 11

Warning: Cannot modify header information - headers already sent by (output started at /home/springbo/public_html/minichat_admin.php:11) in /home/***/public_html/.php on line 20

:banghead:

May be my code should help
Code:
try
{
   $bdd = new PDO('mysql:host=localhost;dbname=***', '***', '***');
}
catch(Exception $e)
{
  die('Erreur : '.$e->getMessage());
}
mysql_query('SET time_zone="-6:00"');
$pseudo = stripslashes(htmlentities($_POST['pseudo'], ENT_COMPAT | ENT_HTML401, 'ISO-8859-1'));
$message = stripslashes(htmlentities($_POST['message'], ENT_COMPAT | ENT_HTML401, 'ISO-8859-1'));
$message = nl2br($message);
$quoi = stripslashes(htmlentities($_POST['quoi'], ENT_COMPAT | ENT_HTML401, 'ISO-8859-1'));
// Insertion du message à l'aide d'une requête préparée
$req = $bdd->prepare('INSERT INTO ****(pseudo, message, quoi, quand) VALUES(?, ?, ?, NOW())');
$req->execute(array($pseudo, $message, $quoi));
 

usama_rasab27

Member
Messages
208
Reaction score
15
Points
18
1. Check that the user has been granted all privileges from CPanel X, from "MySQL databases".
2. It might be because you have mixed MySQL_FUNCTIONS with PDO.

Code:
mysql_query('SET time_zone="-6:00"');
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
You have a PDO connection (not mysql_connect), so you'll have to send the time zone query as a PDO query.
 

springbox99

Member
Messages
114
Reaction score
3
Points
18
Thank you all. Everything is fine now.
Here's the code :
Code:
date_default_timezone_set('Europe/Paris');
$dt=new DateTime();
$offset=$dt->format("P");
try
{
   $bdd = new PDO('mysql:host=localhost;dbname=****', '****', '*****');
   $bdd->exec("SET time_zone='$offset';");
}
catch(Exception $e)
{
  die('Erreur : '.$e->getMessage());
}
etc.
 
Status
Not open for further replies.
Top