0

i have to insert current date & time into my database table in order to track users "when was their last visits"

Currently i am in India. In My windows system, its showing 5/13/2013 5:58 Pm But the below code is showing me the today data & time as 2013-05-13 13:02:28 . Why time is not accurate.

I want to make the timing accurate for all users around the world.

I used the below code . Please help me to find this answer.

   $this->load->database();
   $this->load->helper('date');   
   $datestring = "%Y-%m-%d: %d:%h:%i";

   $time = time(); 
   $today=mdate($datestring, $time);

   echo($today);//showing not accurate time, as i explained it above

If my question is not clear, then please comment below, i wil try to explain you in diffrent ways.

Dan
  • 2,086
  • 11
  • 71
  • 137

1 Answers1

2

It's giving you a UTC time , while Indian Timezone is UTC + 5.30. So you need to set your timezone to Asia/Calcutta in your root index or config file.

date_default_timezone_set("Asia/Calcutta");

Also you need to change your $dateString.

$datestring = "%Y-%m-%d: %H:%i:%s";
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • Thank You, but again if i change this, then the users who are not from asia/Calcutta , then for them it will show wrong time? I want to show to make this in such a way that it should give correct time for all users around the world? What you suggest? – Dan May 13 '13 at 12:38
  • @Ashutosh - Than just change the timezone on fly. – Rikesh May 13 '13 at 12:42
  • Did you mean i have to change the timezone based on users ip address dynamically whenever the do any activities? . And i will store the timezone name in my db ? – Dan May 13 '13 at 12:44
  • In a way. [Read More](http://stackoverflow.com/questions/13/determining-a-web-users-time-zone). – Rikesh May 13 '13 at 12:45
  • Sorry, i tried your above code `date_default_timezone_set("Asia/Calcutta");` and i kept it in my root index.php but also the same problem. And also i tested it in my config.php but also same problem – Dan May 13 '13 at 13:22
  • Check [here](http://codepad.viper-7.com/JWzWjt). Your `$dateString` is incorrect. – Rikesh May 13 '13 at 13:29
  • I am trying to implement this http://stackoverflow.com/questions/13/determining-a-web-users-time-zone/1809974#1809974 to accoplish my need. But i am confuse, how can i put this in my config.php file , and call `-new Date().getTimezoneOffset()/60;` this one. Even the answer is not in simple. Please help me to do this. – Dan May 13 '13 at 14:39
  • It's probably easiest to store the time as UTC and convert it on output. You can use CI's `local_to_gmt` and `gmt_to_local` to do the conversion. Or you can use PHP's `DateTime`/`DateTimezone` classes. – mcrumley May 13 '13 at 15:32
  • @mcrumley so did you mean, first i set `date_default_timezone_set("UTC");` in my config.php and wherever i am inserting date there i will use this function `gmt_to_local`? – Dan May 13 '13 at 16:04
  • `date_default_timezone_set` doesn't really matter, but it should be set to something reasonable (the timezone your server is set to). `gmt_to_local` takes an argument that is the user's timezone. That will need to be set per user and stored in their profile. – mcrumley May 13 '13 at 19:21