How to get current date, given a timezone in PHP
PHP
date()
function is an in-built function that simplifies working with date data types. The PHP date()
function is used to format a date/time into a human-readable format.In this tutorial, We will show you how to get the current date, given a timezone in PHP. Using PHP, you can get and display the current Date and Time from a specific timezone.
- Create a new DateTime object with PHP.
- Convert the DateTime object to the timezone of New_York using the
DateTimeZone
object. - Specify the date and time format.
Here’s an example of how to do this:
1 2 3 4 5 6 7 |
// Set the timezone to "America/New_York" $date = new DateTime("now", new DateTimeZone('America/New_York') ); // Get the current date and time in the specified timezone $dateTime = $date->format('Y-m-d H:i:s'); echo $dateTime; |
You can change the timezone in the DateTimeZone object and adjust the date format in the
format()
function.