Garayed.com  

Go Back   Garayed.com > PHP
FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-25-2006, 02:41 AM
windandwaves
 
Posts: n/a
Default i love strtotime but it is no use in New Zealand

Hi Folk

I love the strtotime function, because I can make a date field in a form and
just tell my users to enter whatever they want and as long as it is a date,
it will work.

HOWEVER, this obviously does not work for the day/month vs month/day
scenario. That is, in New Zealand we use day/month/year, while the function
assumes it to be month/day/year.

Does anyone have a simple function that helps me close that loophole (I am
using PHP 4.4).

I copied the function below from php.net, but I dont think it will work in
all cases.

Cheers

> Nicolaas

# Returns a timestamp from a string based on the given format and default
timezone if it's ambiguous.
# %Y - year as a decimal number including the century
# %m - month as a decimal number (range 01 to 12)
# %d - day of the month as a decimal number (range 01 to 31)
# %H - hour as a decimal number using a 24-hour clock (range 00 to 23)
# %M - minute as a decimal number

function parseDate( $date, $format = "%d/%m/%Y") {
// Builds up date pattern from the given $format, keeping delimiters in
place.
if( !preg_match_all( "/%([YmdHMp])([^%])*/", $format, $formatTokens,
PREG_SET_ORDER ) ) {
return false;
}
foreach( $formatTokens as $formatToken ) {
$delimiter = preg_quote( $formatToken[2], "/" );
$datePattern .= "(.*)".$delimiter;
}

// Splits up the given $date
if( !preg_match( "/".$datePattern."/", $date, $dateTokens) ) {
return false;
}
$dateSegments = array();
for($i = 0; $i < count($formatTokens); $i++) {
$dateSegments[$formatTokens[$i][1]] = $dateTokens[$i+1];
}

// Reformats the given $date into US English date format, suitable for
strtotime()
if( $dateSegments["Y"] && $dateSegments["m"] && $dateSegments["d"] ) {
$dateReformated =
$dateSegments["Y"]."-".$dateSegments["m"]."-".$dateSegments["d"];
}
else {
return false;
}
if( $dateSegments["H"] && $dateSegments["M"] ) {
$dateReformated .= " ".$dateSegments["H"].":".$dateSegments["M"];
}

return strtotime( $dateReformated );
}


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 09:26 AM.




LinkBacks Enabled by vBSEO 3.0.0 © 2007, Crawlability, Inc.