Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display time/date in specific timezone using date() function

Tags:

php

I use the date() function to get day, month and year.

$year = date(y);
$month  = date(m);
$day = date(d);

But my hosting is in another place where I am, so I need to add 11 hours. could you tell me how can I do that? thanks

like image 377
Simon Avatar asked Apr 09 '10 12:04

Simon


People also ask

How do you create a date in a specific time zone?

Use the toLocaleString() method to initialize a date with time zone, e.g. date. toLocaleString('en-US', { timeZone: 'America/Los_Angeles'}) . The method can be passed the locale and the time zone as parameters and returns a string that represents the date according to the provided values.

How to display time according to timezone in php?

For specific time zone select you can use date_default_timezone_set before using date() or time() function. And to specify the timezone : $now = new DateTime(null, new DateTimeZone('Asia/Dhaka')); $now->setTimezone(new DateTimeZone('Asia/Dhaka')); // Another way echo $now->getTimezone();

How to convert date to different timezone in JavaScript?

To convert a date to another time zone: Use the toLocaleString() method to get a string that represents the date according to the provided time zone. Pass the result to the Date() constructor. The returned Date object will have its date and time set according to the provided time zone.


1 Answers

Either do

date('Y-m-d', strtotime('+11 hours'));

to add 11 hours or create a DateTime object and change it's timezone where needed

$datetime = new DateTime; // current time = server time
$otherTZ  = new DateTimeZone('America/Los_Angeles');
$datetime->setTimezone($otherTZ); // calculates with new TZ now

or simply set the appropriate timezone with

  • date_default_timezone_set — Sets the default timezone used by all date/time functions in a script
like image 68
Gordon Avatar answered Sep 29 '22 17:09

Gordon