note 37718 added to function.mktime

From: Date: Mon, 24 Nov 2003 10:36:10 +0000
Subject: note 37718 added to function.mktime
Groups: php.notes 
Request: Send a blank email to php-notes+get-60904@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/function.mktime.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37718 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37718&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37718&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#60904) next »