2

i am building a web app on cakephp 2.2 ..i am newbie in cakephp and have never worked with a time..the problem is i want to show the time where the website is viewing .. for example if someone is viewing my website in Australia,he can see the time according to their country. and i want to show seperate date and time .. don't want to combine time and date

here what i am doing right now

in AppController i have done this

 public function beforeRender(){
   if ($this->Auth->login() || $this->Auth->loggedIn()) {
     App::uses('CakeTime', 'Utility');

  }}

dont know how to echo as well

hellosheikh
  • 2,929
  • 8
  • 49
  • 115

2 Answers2

2

We can find a client location with out using database, for this we need to use some api, example I used the host api.

Controller code :

            $clientIpAddress = $this->request->clientIp();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://api.hostip.info/get_html.php?ip=$clientIpAddress&position=true");
    curl_setopt($ch, CURLOPT_HEADER,0);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);

$data will return a sample below: Country: UNITED STATES (US) City: Aurora, TX Latitude: 33.0582 Longitude: -97.5159 IP: 12.215.42.19

So you got the location from this $data.

Use this in view file as follows :

$this->Time->format('F jS, Y h:i A',date('M d, Y h:i:s'), null,'Aurora, TX');

But dont forget write the city name, ip address values in session, so no need to send the curl request every time a page loads for the single user. Just first time find the location and ip address and write in session then use it

AnNaMaLaI
  • 4,064
  • 11
  • 53
  • 93
  • it isn't working .i write your controller code into my beforeRender function in my AppController..and same for the view i write your code ..but it is not showing anything on my view page..when i debug $data .. this is what comes up on my view page theCountry: (Private Address) (XX) City: (Private Address) Latitude: Longitude: IP: ::1 – hellosheikh Jun 24 '13 at 10:10
  • u r checking in ur localhost or server ? – AnNaMaLaI Jun 24 '13 at 10:17
  • Localhost have common ip, so the code will work only on server. In In localhost it will return $data as follows "Country: (Private Address) (XX) City: (Private Address) Latitude: Longitude: IP: 127.0.0.1 " – AnNaMaLaI Jun 24 '13 at 10:28
  • uhhh... how would you expect `Aurora, TX` to resolve to a time zone? It would have to be a [valid IANA time zone id](http://en.wikipedia.org/wiki/List_of_tz_database_time_zones) that PHP supports, would it not? (example, `America/Chicago`). You probably need one of the methods [here](http://stackoverflow.com/q/16086962/634824). Also - geolocation by IP address is not typically a safe way to determine a time zone. You might be getting the time zone of some internet proxy server. – Matt Johnson-Pint Jun 24 '13 at 18:15
  • Hey @Matt Johnson I gave just an example, He need to figure it out. Because we cant sit and write the 100% perfect code for every one right – AnNaMaLaI Jun 25 '13 at 06:19
  • So... Your idea is to suggest something unrelated that doesn't answer the question and hope that he figures it out? Really??? – Matt Johnson-Pint Jun 25 '13 at 12:59
  • Its related answer.. @liyakat gave with database.. I gave without db. Once we given this 2 answers again hellosheikh asked another question check his profile.. My Answer is 90% works free of cost. liyakat answer works 100% perfect with cost – AnNaMaLaI Jun 25 '13 at 13:02
1

An easy way to manage the time is to save all dates and times as GMT+0 or UTC. Uncomment the line date_default_timezone_set('UTC'); in app/Config/core.php to ensure your application’s time zone is set to GMT+0.

Next add a time zone field to your users table and make the necessary modifications to allow your users to set their time zone. Now that we know the time zone of the logged in user we can correct the date and time on our posts using the Time Helper:

echo $this->Time->format('F jS, Y h:i A', $post['Post']['created'], null, $user['User']['time_zone']);

Precondition is that you should know the time zone of users, and that would be better if you will store timezone per register user.

and if you dont want to use as register user you can also track and use some third party service to give timezone from IP Address like ip2location

liyakat
  • 11,825
  • 2
  • 40
  • 46
  • i am not storing the time in database .. i want to just show or echo the time based on country – hellosheikh Jun 24 '13 at 09:36
  • thankyou i got you.. ok so i cameup to a solution that why dont i create a setting page of time zone where user set time.. can you tell me how can i store time in db i mean what format it should be and then how to retrieve it ? – hellosheikh Jun 24 '13 at 09:41
  • 1
    http://stackoverflow.com/questions/4561291/mysql-gmt-dates-and-timezones bothe above comment is very useful to store gmt time and timezone – liyakat Jun 25 '13 at 03:48