Ragith Thomas
Ragith Thomas

Reputation: 105

How to get current date and time in php

Thanks in advance. Actually i was using date('Y-m-d h:i:s', time()) and date("Y-m-d H:i:s") to get the current date and time in php but it's not showing the proper time. Instead of showing proper Indian time it's showing some other time My code is:

$today = date("Y-m-d H:i:s");
echo "Current date and time is ".$today;

Upvotes: 2

Views: 35356

Answers (5)

Dave
Dave

Reputation: 3091

Try this, use date_default_timezone_set (Asia/Kolkata)

date_default_timezone_set("Asia/Kolkata");
$today = date("Y-m-d H:i:s");
echo "Current date and time is ".$today;

DEMO

Upvotes: 3

Faizan Khattak
Faizan Khattak

Reputation: 882

date_default_timezone_set("Asia/Kolkata");
$today = date("Y-m-d H:i:s");
echo "Current date and time is ".$today;

This will help you to find your location: http://php.net/manual/en/timezones.asia.php

Upvotes: 2

devpro
devpro

Reputation: 16117

You need to use date_default_timezone_set for set the specific time zone as:

if (function_exists('date_default_timezone_set'))
{
    date_default_timezone_set('Asia/Mumbai'); // will set the mumbai timezone.
    echo date('Y-m-d H:i:s');
}

This will help you to find your location: http://php.net/manual/en/timezones.asia.php

Demo

Upvotes: 1

Adam Smith
Adam Smith

Reputation: 96

Try this.

date_default_timezone_set('Asia/Kolkata'); 
echo date("Y-m-d H:i:s"); // time in India

Upvotes: 6

Ramalingam Perumal
Ramalingam Perumal

Reputation: 1427

You can use date_default_timezone_set for set Indian time:

    date_default_timezone_set('Asia/Kolkata'); //Indian time.
    echo date('Y-m-d H:i:s');

Upvotes: 1

Related Questions