Jump to content

[SOLVED] Getting a reliable dat using php


DamienRoche

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/124925-solved-getting-a-reliable-dat-using-php/
Share on other sites

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');

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();

 

?

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.

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");
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.