Get Local Date-Time given a time difference

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
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Get Local Date-Time given a time difference

Post by n00b Saibot »

This function provides you with the local timestamp given a GMT time difference.

Code: Select all

<?php
/////////////////////////////////////////////////////////////////
// Local Timestamp function                                    //
// Written by n00b Saibot                                      //
// Syntax:                                                     //
//       int returnLocalTime ( string GMTDiff )                //
// Description :                                               //
//       provides Local Timestamp given a GMT Time difference. //
// Usage :                                                     //
//       $localTimeStamp = returnLocalTime("+0530");           //
/////////////////////////////////////////////////////////////////

function returnLocalTime($GMTDiff="+0000")
{
 $sign = substr($GMTDiff,0,1); //get sign
 $hr = substr($GMTDiff,1,2);  //get hours
 $min = substr($GMTDiff,3); //get minutes
 switch($sign)
 {
  case '+': 
   return mktime(gmdate('H')+$hr, gmdate('i')+$min, gmdate("s"), gmdate("m"), gmdate("d"), gmdate("Y"));
   break;
  case '-':
   return mktime(gmdate('H')-$hr, gmdate('i')-$min, gmdate("s"), gmdate("m"), gmdate("d"), gmdate("Y"));
   break;
 } //end switch
} //end function

?>
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Note: The above doesn't take into account DST/Summer time.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

How do you know whether we're on a day light saving period or not?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

daylight status is governed by a variety of things depending on location.. if you google for "timezone daylight rules" you may find out what I'm referring to..
Post Reply