0

I am working on an application in which I need to get date & time of local pc of user. Basically php have date and time functions and it returns server time as it is server side scripting language.

I am getting correct time using javascript Date function but when i use strtotime function in php it doesn't work, i need a way to get local time in php.

<script type="text/javascript">

function getDate() 
{ 
        var d = new Date();
    var c_day = d.getDate();
    var c_month = d.getMonth() + 1;
    var c_year = d.getFullYear();
    var c_hour = d.getHours(); 
    var c_min = d.getMinutes(); 
    var t = c_day+"-"+c_month+"-"+c_year+" "+c_hour+":"+c_min;
    return t;
}
if (window.XMLHttpRequest)
{
    xmlhttp=new XMLHttpRequest();
}
else
{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    { 
        alert(xmlhttp.responseText);        
    }
}    
xmlhttp.open("GET","setdate.php?sdate="+encodeURIComponent(getDate()),false);
xmlhttp.send();
</script>

The file 'setdate.php' stores sdate value in the session and i am getting it as an output of ajax.

<?php
$sdate = $_SESSION['sdate'];
$date = date( "d/m/Y", strtotime( $sdate ) );
?>

This works after refreshing the page, but not when the page loads first time.

Any one can help me on this ?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Kcs Dipak
  • 43
  • 1
  • 2
  • 6
  • try this date('m/d/Y'); – chirag ode Sep 21 '13 at 08:39
  • possible duplicate of http://stackoverflow.com/questions/13/determining-a-web-users-time-zone http://stackoverflow.com/questions/5203382/how-to-automatically-detect-users-timezone http://stackoverflow.com/questions/1091372/getting-the-clients-timezone-in-javascript – Glavić Sep 21 '13 at 09:01

5 Answers5

3

PHP is a server side language, which means that you won't be able to get any of the client's data at all unless they send it to you (via headers, for instance, like the user agent). That said, you could just use Javascript to get the client/browser date and then send it back to PHP (via AJAX, a redirection with this data in the URL...).

By the way, consider adding the timezone to your data. Nobody does usually care about it, and it's the root of all evil. Really.

Diego Agulló
  • 9,298
  • 3
  • 27
  • 41
  • Thanks Diego, I just implemented ajax and sent the java script date to php file to store it in session, it stored the local date into php session. Here, i have to refresh the page atleast one time to get the session value, i don't get the session value first time. Any idea on this ? – Kcs Dipak Sep 21 '13 at 09:36
  • Show us the code, then we may be able to find the problem. :P – Diego Agulló Sep 21 '13 at 09:57
2

You can not get Client Side Date using php alone.

For client side, you would need Javascript, something like the following should do the trick.

var currentTime = new Date();

You can use AJAX to send this to server.

var currentTime = new Date();
$.ajax({
    url : url,
    data : {date : currentTime}
});
Ashwini Agarwal
  • 4,828
  • 2
  • 42
  • 59
  • 1
    For any kind of time sensitive information though this would not suffice as the user can change their clock to reflect whatever they want. – rfoo Oct 09 '13 at 20:14
0

Use javascript's new Date().getTime() to get the time from client. Deliver time to server and then use php's strftime($format, $clientTime) to format the time.

Sami Korhonen
  • 1,254
  • 8
  • 17
0

There's no reliable way to get the users time via the server. You can pass the users time via javascript to php but it is not a secure way of doing things. I'd say the best way is to determine the users timezone and just set date_default_timezone_set() appropriately.

rfoo
  • 1,160
  • 1
  • 12
  • 27
  • 1
    Sounds good, I will definitely try this solution and share my feedback. Thanks rosscowar. – Kcs Dipak Sep 21 '13 at 09:45
  • Awesome! There are various services and databases out there to help automatically determine a users time zone based on their ip address. If this is sensitive information be sure the user has a way to change it though. Best of luck! – rfoo Sep 21 '13 at 09:52
  • 1
    I get the localtimezone than set the server time zone using date_default_timezone_set and its working fine. Thanks. – Kcs Dipak Sep 23 '13 at 04:17
0

Thanks for your suggestions.

Finally I have solved it by using the java script to get the local timezone and set the cookies using java script, removed the PHP from the flow... :)

Kcs Dipak
  • 43
  • 1
  • 2
  • 6