Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current date, given a timezone in PHP?

Tags:

date

timezone

php

I want to get todays date given a time zone in Paul Eggert format(America/New_York) in PHP?

like image 502
einstein Avatar asked Nov 04 '11 08:11

einstein


People also ask

How can I get current date and time in PHP?

Answer: Use the PHP date() Function You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.

How can I get timezone in PHP?

The date_default_timezone_get() function returns the default timezone used by all date/time functions in the script.

How can I get current date in dd mm yyyy format in PHP?

php /*print date in dd/mm/yy format*/ print "Current date in dd/mm/yy format: " . date("d/m/y"); print "</br>"; /*print date in dd/mm/yyyy format*/ print "Current date in dd/mm/yyyy format: " . date("d/m/Y"); print "</br>"; /*print date in dd MON yyyy format*/ print "Current date in dd MON yyyy format: " .

How get fetch date from database in PHP?

$d=mktime(11, 14, 54, 8, 12, 2014); echo "Created date is " . date("Y-m-d h:i:sa", $d);


2 Answers

Set the default time zone first and get the date then, the date will be in the time zone you specify :

<?php   date_default_timezone_set('America/New_York');  $date= date('m-d-Y') ;  ?> 

http://php.net/manual/en/function.date-default-timezone-set.php

like image 38
Ghost-Man Avatar answered Oct 01 '22 13:10

Ghost-Man


The other answers set the timezone for all dates in your system. This doesn't always work well if you want to support multiple timezones for your users.

Here's the short version:

<?php $date = new DateTime("now", new DateTimeZone('America/New_York') ); echo $date->format('Y-m-d H:i:s'); 

Works in PHP >= 5.2.0

List of supported timezones: php.net/manual/en/timezones.php


Here's a version with an existing time and setting timezone by a user setting

<?php  $usersTimezone = 'America/New_York'; $date = new DateTime( 'Thu, 31 Mar 2011 02:05:59 GMT', new DateTimeZone($usersTimezone) ); echo $date->format('Y-m-d H:i:s'); 

Here is a more verbose version to show the process a little more clearly

<?php  // Date for a specific date/time: $date = new DateTime('Thu, 31 Mar 2011 02:05:59 GMT');  // Output date (as-is) echo $date->format('l, F j Y g:i:s A');       // Output line break (for testing) echo "\n<br />\n";  // Example user timezone (to show it can be used dynamically) $usersTimezone = 'America/New_York';  // Convert timezone $tz = new DateTimeZone($usersTimezone); $date->setTimeZone($tz);  // Output date after  echo $date->format('l, F j Y g:i:s A'); 

Libraries

  • Carbon — A very popular date library.
  • Chronos — A drop-in replacement for Carbon focused on immutability. See below on why that's important.
  • jenssegers/date — An extension of Carbon that adds multi-language support.

I'm sure there are a number of other libraries available, but these are a few I'm familiar with.


Bonus Lesson: Immutable Date Objects

While you're here, let me save you some future headache. Let's say you want to calculate 1 week from today and 2 weeks from today. You might write some code like:

<?php  // Create a datetime (now, in this case 2017-Feb-11) $today = new DateTime();  echo $today->format('Y-m-d') . "\n<br>"; echo "---\n<br>";  $oneWeekFromToday = $today->add(DateInterval::createFromDateString('7 days')); $twoWeeksFromToday = $today->add(DateInterval::createFromDateString('14 days'));  echo $today->format('Y-m-d') . "\n<br>"; echo $oneWeekFromToday->format('Y-m-d') . "\n<br>"; echo $twoWeeksFromToday->format('Y-m-d') . "\n<br>"; echo "\n<br>"; 

The output:

2017-02-11  ---  2017-03-04  2017-03-04  2017-03-04 

Hmmmm... That's not quite what we wanted. Modifying a traditional DateTime object in PHP not only returns the updated date but modifies the original object as well.

This is where DateTimeImmutable comes in.

$today = new DateTimeImmutable();  echo $today->format('Y-m-d') . "\n<br>"; echo "---\n<br>";  $oneWeekFromToday = $today->add(DateInterval::createFromDateString('7 days')); $twoWeeksFromToday = $today->add(DateInterval::createFromDateString('14 days'));  echo $today->format('Y-m-d') . "\n<br>"; echo $oneWeekFromToday->format('Y-m-d') . "\n<br>"; echo $twoWeeksFromToday->format('Y-m-d') . "\n<br>"; 

The output:

2017-02-11  ---  2017-02-11  2017-02-18  2017-02-25  

In this second example, we get the dates we expected back. By using DateTimeImmutable instead of DateTime, we prevent accidental state mutations and prevent potential bugs.

like image 62
Andy Fleming Avatar answered Oct 01 '22 15:10

Andy Fleming