简体   繁体   中英

Get current IST time in php

I am using the following code to get current IST time. But it only gives the system time. I changed my system time due to some reason. So i get only system time instead of current time.

$time_now=mktime(date('h')+5,date('i')+30,date('s'));
$date = date('d-m-Y H:i', $time_now);
echo $date;

Set your system time correctly, so your system knows its correct timezone and the correct time there. In PHP, set your desired timezone, then just print the date:

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

Don't do manual offset calculations unless you know exactly what you're doing, it's way too fickle.

Get exact Current Indian Time in PHP

$date = date_default_timezone_set('Asia/Kolkata');

//If you want Day,Date with time AM/PM
echo $today = date("F j, Y, g:i a T");

//Get Only Current Time 00:00 AM / PM 
echo $today = date("g:i a");

Use now() method it gave current time.

$query = "UPDATE (table name) SET (column) = now() WHERE (condition)";

Note : Make sure that type of the column in the database must be DATETIME type.

To get the user current time you will need javascript. if you want to make default timezone on your pages you can use date_default_timezone_set

You can read about this in: HERE

Use date_default_timezone_set :

if (function_exists('date_default_timezone_set'))
{
  date_default_timezone_set('Asia/Kolkata');
}

echo date('Y-m-d h-i-s');

Notice: date_default_timezone_set() : Timezone ID 'Asia/Mumbai' is invalid in D:\\Program Files\\wamp\\www\\PHP\\...

Only date_default_timezone_set('Asia/Kolkata') is Showing Valid In PHP 5.4.16 Ver :) :)

// set the timezone first
if(function_exists('date_default_timezone_set')) {
    date_default_timezone_set("Asia/Kolkata");
}

$date = date("m/d/Y");
echo $date;

Before adding the date function use the timezone declaration first.

Set your system time correctly, so your system knows its correct timezone and the correct time there. In PHP, set your desired timezone, then just print the date:

date_default_timezone_set('Asia/Kolkata'); echo date('dmY H:i');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM