10

I need to show current time on my site in my city Iran/Tehran

I'm using this code:

 <?php echo date('H:i'); ?> 

but it doesn't show the right time.

my country timezone : iran/tehran

my city time now is : 17:45

but on the site it shows me : 23:15

I don't know what time zone my server is in & I can't change that, please help me.

MohammadHossein R
  • 1,249
  • 1
  • 16
  • 30
HamidZ
  • 150
  • 1
  • 2
  • 11
  • This is askt so many times... For example: http://stackoverflow.com/questions/10503673/strftime-function-showing-incorrect-time/10503716#10503716 – Laurence Jan 07 '13 at 14:26

4 Answers4

11

Try to change timezone in you code like that:

date_default_timezone_set('you/timezone');
KryDos
  • 447
  • 4
  • 14
7

You can use date_default_timezone_set or DateTimeZone

Example

echo date('H:i'),PHP_EOL;

date_default_timezone_set('Iran');
echo date('H:i'),PHP_EOL;

//Or

$date = new DateTime("now", new DateTimeZone("Asia/Tehran"));
echo $date->format("H:i"),PHP_EOL;

Output

14:24 
17:54
17:54
Baba
  • 94,024
  • 28
  • 166
  • 217
0

You can set it by adding or editing a .htaccess file in the document root folder of your website and adding this line php_value date.timezone (your timezone here)

user1620152
  • 134
  • 2
  • 14
0

There is very elegant way of dealing with this, namely: DateTimeZone::getOffset. You may find detailed usage exaples here: http://php.net/manual/en/datetimezone.getoffset.php

Eduárd Moldován
  • 1,495
  • 3
  • 13
  • 29