note 39878 added to function.gmmktime

From: Date: Fri, 13 Feb 2004 20:40:44 +0000
Subject: note 39878 added to function.gmmktime
Groups: php.notes 
Request: Send a blank email to php-notes+get-65116@lists.php.net to get a copy of this message
When attempting to use HTTP's If-Modified-Since features for caching I ran into the problem of being able to compare the GMT date the browser was sending to my own Last-Modified date (stored in a database field). I saw many examples of how to create a GMT date from a unix timestamp, but little on how to actually get a GMT date into a unix timestamp. Perhaps someone has a better way, here's my solution: <?php function gmstrtotime($sgm) { $months = array( 'Jan'=>1, 'Feb'=>2, 'Mar'=>3, 'Apr'=>4, 'May'=>5, 'Jun'=>6, 'Jul'=>7, 'Aug'=>8, 'Sep'=>9, 'Oct'=>10, 'Nov'=>11, 'Dec'=>12 ); list($D, $d, $M, $Y, $H, $i, $s) = sscanf($sgm, "%3s, %2d %3s %4d %2d:%2d:%2d GMT"); return gmmktime($H, $i, $s, $months[$M], $d, $Y); } // test: after all is said and done // $time should be the same as $gmtime $time = time(); $us = date("m/d/Y H:i:s",$time); $sgm = gmdate("D, d M Y H:i:s",$time) . " GMT"; $gmtime = gmstrtotime($sgm); echo $us . "<BR>"; echo $sgm . "<BR>"; echo $time . "<BR>"; echo $gmtime . "<BR>"; ?> My results: 02/13/2004 10:45:42 Fri, 13 Feb 2004 20:45:42 GMT 1076705142 1076705142 Credit to kyle at frozenonline dot com for his strtotime example ---- Manual Page -- http://www.php.net/manual/en/function.gmmktime.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39878 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39878&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39878&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#65116) next »