DamienRoche Posted September 19, 2008 Share Posted September 19, 2008 I'm trying to find the day of the month for users. I want it to obviously be their local time, not the servers. How do I go about doing this in a reliable way? I've heard you can use date(); and also date_default_timezone_set(); Which would you advise me to use and how do I use them just to get the day of the month? Can I get what date is set on the user's computer? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/124925-solved-getting-a-reliable-dat-using-php/ Share on other sites More sharing options...
aebstract Posted September 19, 2008 Share Posted September 19, 2008 For the date on the user's computer, can't they look in the corner of their screen. Not to sound like a smart ass but if you're just trying to let them know the date it is kind of pointless. However, this is what you would use for the current day: date('l'); Quote Link to comment https://forums.phpfreaks.com/topic/124925-solved-getting-a-reliable-dat-using-php/#findComment-645499 Share on other sites More sharing options...
Aimee Posted September 19, 2008 Share Posted September 19, 2008 I think day of the month is date('d') - date('l') is day of the week. However this will be local to the server time not the users. I believe you have to use javascript to get the local time for the user. var d = new Date() var day = d.getDate(); ? Quote Link to comment https://forums.phpfreaks.com/topic/124925-solved-getting-a-reliable-dat-using-php/#findComment-645505 Share on other sites More sharing options...
aebstract Posted September 19, 2008 Share Posted September 19, 2008 err yeah, I was thinking he wanted a the word for the day of the month. My bad Quote Link to comment https://forums.phpfreaks.com/topic/124925-solved-getting-a-reliable-dat-using-php/#findComment-645507 Share on other sites More sharing options...
DamienRoche Posted September 19, 2008 Author Share Posted September 19, 2008 Thanks for the replies guys. I didn't think it'd be so hard to get the local time for the user. Can I not find out the timezone for the user? then use date_timezone_set(); The reason for all of this is because I'm writing a "...of the day" script and need a reliable date. Quote Link to comment https://forums.phpfreaks.com/topic/124925-solved-getting-a-reliable-dat-using-php/#findComment-645517 Share on other sites More sharing options...
PFMaBiSmAd Posted September 19, 2008 Share Posted September 19, 2008 See my last post in this thread - http://www.phpfreaks.com/forums/index.php/topic,216673.0.html Or a slightly simplified version of that code - <?php session_start(); if(!isset($_SESSION['time_zone'])){ // if the time zone is not known, get it from the client if (isset($_GET['offset'])) { $minutes = $_GET['offset']; $hours = $minutes/60; $tz = sprintf('%+d', $hours); $_SESSION['time_zone'] = $tz; // the following four lines are for demonstration purposes and can be removed echo "GMT offset (in minutes, from the browser): $minutes, Timezone = Etc/GMT$tz<br />\n"; echo "GMT: ". gmdate("Y-m-d H:i:s") ."<br />\n"; $local = gmmktime(gmdate("H"),gmdate("i")-$minutes); // adjust GMT by client's offset echo "Calculated client's date/time: ". gmdate("Y-m-d h:i:s a",$local) ."<br />\n"; } else { echo "<script language='javascript'>\n"; echo "var d = new Date();\n"; echo "location.href=\"${_SERVER['SCRIPT_NAME']}?offset=\" + d.getTimezoneOffset();\n"; echo "</script>\n"; exit(); } } date_default_timezone_set("Etc/GMT{$_SESSION['time_zone']}"); echo "Server time by setting timezone: " . date("h:i:s a"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/124925-solved-getting-a-reliable-dat-using-php/#findComment-645530 Share on other sites More sharing options...
DamienRoche Posted September 19, 2008 Author Share Posted September 19, 2008 I don't know what you just did but that worked!! Thank you so much!! Quote Link to comment https://forums.phpfreaks.com/topic/124925-solved-getting-a-reliable-dat-using-php/#findComment-645560 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.