I just got done working on a project where the client wanted to display content on the site at 3pm Eastern Standard Time (EST) every day. While working on the query I realized that my server time was using UTC time so I needed to figure out a way to change to EST.
I was able to accomplish this using the PHP DateTime() Class. Take a look below:
| <?php | |
| // This will output current time in EST | |
| $timezone = 'EST'; | |
| $timestamp = time(); | |
| $datetime = new DateTime("now", new DateTimeZone($timezone)); //first argument "must" be a string | |
| $datetime->setTimestamp($timestamp); //adjust the object to correct timestamp | |
| echo '<b>Current Time: </b>' . $datetime->format('Ymd ga e'); | |
| echo '<br />'; | |
| // This will output current time + 24 hours in EST | |
| $timezone = 'EST'; | |
| $timestamp = time()+86400; | |
| $datetime = new DateTime("now", new DateTimeZone($timezone)); //first argument "must" be a string | |
| $datetime->setTimestamp($timestamp); //adjust the object to correct timestamp | |
| echo '<b>+24 hours:</b> ' . $datetime->format('Ymd ga e'); | |
| ?> |
You just need to update the $timezone variable with the desired timezone and the desired date output format and you’re good to go!