Returning Users Local Time

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Returning Users Local Time

Post by s.dot »

This function requires a parameter, which is the offset (in hours) from the server time. It will generate the time according to their settings (that they have specified, perhaps in a db?).

Code: Select all

<?php
function returnUserTime($offset){
	if($offset != 0){
		return date("F jS, Y \a\\t g:i A",time()+($offset*60*60));
	} ELSE {
		return date("F jS, Y \a\\t g:i A");
	}
}
?>
Examples of usage:

Code: Select all

<?php

echo returnUserTime(-3);
  // user is 3 hours behind the server time
  // returns the time from 3 hours ago

echo returnUserTime(3);
  // doesn't need a + sign,
  // returns users time 3 hours ahead of the server time
?>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Alternatively, if you have a date stored in a database that you'd like to convert to a users time that they have set, you can pass a timestamp parameter

Code: Select all

function returnUserTime($offset,$timestamp){ 
   if($offset != 0){ 
        return date("F jS, Y \a\\t g:i A",$timestamp+($offset*60*60)); 
    } ELSE { 
        return date("F jS, Y \a\\t g:i A",$timestamp); 
    } 
}

echo returnUserTime(-2,$time_stamp_from_db);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

edit - nvm (needs to read more carefully)
Post Reply