zarkoz
zarkoz

Reputation: 369

date() function in PHP and its 2nd parameter

PHP date() documentation says:

timestamp The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().

I'm using server located in Belgrade (timezone in php.ini is "Europe/Belgrade") which is UTC+2 currently:

echo date('F j, Y, H:i - e') // result => March 30, 2016, 18:22 - Europe/Belgrade

Now I would like to get UTC time. If we know that time() always defaults to UTC time and it's same on all servers, I should be able to do this to get UTC time:

echo date('F j, Y, H:i - e', time()); // result => March 30, 2016, 18:22 - Europe/Belgrade

but result is the same. I was supposed to get March 30, 2016, 16:22 - UTC. In docs it says:

The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given

but if it's given, it should default to what's given, isn't that right?

Upvotes: 1

Views: 628

Answers (2)

kator
kator

Reputation: 959

In date('F j, Y, H:i - e', time());, time() is still resolving to your current server time which from your first code is March 30, 2016, 18:22 - Europe/Belgrade. Therefore, essentially, your code is still the same; as date('F j, Y, H:i - e' [, $timestamp = time()]) is treated as date('F j, Y, H:i - e', time()); since there is no second parameter. To get UTC time, I will suggest you use echo gmdate('F j, Y, H:i - e'); //will output March 30, 2016, 16:22 - UTC rather

Upvotes: 2

Aparna
Aparna

Reputation: 255

Yeah, agreed with @KBJ answer or, you can set the timezone of wherever(Asia in your case) you want to get time and then get time.
date_default_timezone_set('Asia/Kolkata');
$date= date('m-d-Y') ;

Upvotes: 1

Related Questions