Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Return Correct Time

Tags:

php

time

For the life of me I can't figure out how to return the current time in PHP correctly. First let me show you the setup.

Server local time is in CST

I am in PST so there is a 2 hour difference. I also put in the htaccess of my server the following statement:

SetEnv TZ Amercia/Los_Angeles

Ok and my PHP statement to pull the current time is

<?php $t = time(); echo(date("D F d Y H:i:s",$t)); ?>

But this code seem to return 8 hours ahead...I can't figure out why?? I want it to return my PST time...

Thanks guys!


2 Answers

The time function returns universal time (UTC). If you want the local time, use localtime.

like image 149
Charley Avatar answered Apr 30 '26 14:04

Charley


Use this function in your script.

date_default_timezone_set('America/Los_Angeles');

List of supported timezones

like image 40
RusAlex Avatar answered Apr 30 '26 14:04

RusAlex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!