user1051505
user1051505

Reputation: 962

Want to know how date() works

Hi I am trying a piece of code wherein I need the exact time.

<?php

date_default_timezone_set('Asia/Kolkata');
echo date("y:d:m:H:i:s");

?>

It shows as the o/p 12:11:04:12:31:24 which is almost 2:30 hours behind the exact time. I am in Mumbai and i want the exact current time. Any suggestions?

Upvotes: 0

Views: 82

Answers (1)

Dutchie432
Dutchie432

Reputation: 29160

As RayMoonDay stated above:

Try

date_default_timezone_set('Asia/Calcutta');

Check php.net/manual/en/timezones.asia.php

I have not tested it - I am just putting it in answer form. If RayMoonDay posts this as an answer, I will edit this to remove that information.

However, I feel compelled to advise you against using date() and familiarize yourself with the DateTime class. date() may be fine for your application. but it has some known issues, especially with future dates.

DateTime: http://php.net/manual/en/class.datetime.php

date_default_timezone_set('Asia/Calcutta');

$d = new DateTime();
echo $d->format("y:d:m:H:i:s");

Upvotes: 1

Related Questions