note 37718 deleted from function.mktime by derick

From: Date: Sat, 26 Jul 2008 10:00:50 +0000
Subject: note 37718 deleted from function.mktime by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142761@lists.php.net to get a copy of this message
Note Submitter: sun4php at coolguide dot net ---- Very often we are faced with the problem of adjusting ourself to local time where application is running rather than running our application at server time. Now we have a way to do that by setting TZ variable using putenv() method. But due to the non-availability of standard time zone codes, developers are often faced with the ambiguity of how to implement a non standard time zone(those which are not too popular) I figured a way out myself for this. It uses the information that a particular time zone is n hours ahead or n hours behind standard GMT time zone. here is what i did. <?php $day=15; $month=12; $year=2003; //convert it to unix timestamp $deadline=mktime('','','',$month,$day,$year); //caluculate our time from GMT time $hour=gmdate('H'); $min=gmdate('i'); $sec=gmdate('s'); $month=gmdate('n'); $day=gmdate('d'); $year=gmdate('Y'); $stamp=mktime ($hour,$min,$sec,$month,$day,$year); // technique is to convert the GMT date & time to unix stamp, then convert time zone //specific information like add 5 hours or subtract 5 hours to seconds,(consider day //light saving here), and add/subtract it to the unix time stamp and then convert //it back to date using date function if needed or perform operations needed on the // time stamp //subtract 6/n hours from GMT time (Central standard time/CST) //vary this part to whatever time zone you are at $do=6*60*60; $lstamp=$stamp-$do; echo("Today(local date/Central):".date('h:i:s d/n/Y',$lstamp)."<br>"); $left=$deadline-$lstamp; $days_left=floor($left/(24 * 60 * 60)); $hours_left=floor($left/(60 * 60 )); echo "<br>Days Left: $days_left<br>"; echo "<br>Hours Left(approx): $hours_left<br>"; ?> Change the $do variable to whatever seconds you are different from GMT and add or subtract accordingly to generate $lstamp. This would solve your time zone problems. Happy Coding, Suneel Kanuri

« previous php.notes (#142761) next »