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
Share on other sites

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

 

?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.