473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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 );
}
Apr 25 '06 #1
18 1946
windandwaves wrote:
it could probably be as simple as writing a regular expression that changes
around anything like
11/12/2006 to 12/11/2006
and
11-12-2006 to 12-11-2006
and
27-05-2006 to 05-27-2006
but leaves
05-30-2006, because 30 > 12 and therefore is the day in the right place.

TIA
Nicolaas

Apr 25 '06 #2

windandwaves wrote:
windandwaves wrote:
it could probably be as simple as writing a regular expression that changes
around anything like
11/12/2006 to 12/11/2006
and
11-12-2006 to 12-11-2006
and
27-05-2006 to 05-27-2006
but leaves
05-30-2006, because 30 > 12 and therefore is the day in the right place.

TIA
Nicolaas


There is no real way of knowing if someone ment Dec. or Nov. if they
enter 12/11/2006. I think your solution lies in providing seperate form
fields for day month and year, OR providing a key that says the desired
format (dd/mm/yyyy)

-Andy

Apr 25 '06 #3
windandwaves wrote:
This is what I came up with:

function eurostrtotime($d) {
// regular expression version
if
(preg_match('!(0[1-9]|[1-2]{1}[0-9]{1}|3[0-1]{1})/(0[1-9]{1}|1[0-2]{1})/(19[0-9]{1}[0-9]{1}|200[0-9]{1})!',
$d, $matches)) {
list(,$d,$m,$y) = $matches;
$ts = mktime(0,0,0,$m,$d,$y);
}
else {
$ts = strtotime($d);
}
return $ts;
}
Can anyone tell me how to include other formats in the regular expression.

Right now, it picks up 27/05/2006, but I would like it to include 27/05/06
and the like.

TIA
Nicolaas

Apr 25 '06 #4
li************@gmail.com wrote:
....
There is no real way of knowing if someone ment Dec. or Nov. if they
enter 12/11/2006. ....

There is, because all users are from New Zealand and in New Zealand no-one
in the right frame of mind would enter 12/11/2006 if they meant Dec 11 2006
and if they did then they would understand it was their mistake and enter it
correctly next time. On the other hand, if the web application would
interpret their date in the US format then they would be disappointed with
the site as it did not cater for the New Zealand format.

What I like about strtotime is that you do not have to have some set format.
You can just enter tomorrow, next week or whatever and it will pick it up.
I think that is just awesome!

Thanks for your reply.
Nicolaas

Apr 25 '06 #5
On Tue, 25 Apr 2006 13:41:31 +1200, windandwaves wrote:
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.


Have you considered moving to Wyoming or Iowa? No waves there, but no
great white sharks, either. Alternatively, you can use PEAR::Date class
which contains different functions to format dates and time.

--
http://www.mgogala.com

Apr 25 '06 #6
windandwaves wrote:
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.


Generally what I do is something like this (pseudo-code):

<?php

function mystrtotime($date)
{
if ($date is "??/??/??" || $date is "??-??-??")
{
list($d, $m, $y) = preg_split('/\/\-/', $date);
if ($y<70)
$y += 2000;
else
$y += 1900;
}
elseif ($date is "??/??/????" || $date is "??-??-????")
list($d, $m, $y) = preg_split('/\/\-/', $date);

if (isset($d))
$date = sprintf("%04d-%02d-%02d", $y, $m, $d);

return strtotime($date);
}
?>

I'll let you work out the regular expressions for the conditionals
yourself. :-)

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Apr 25 '06 #7
windandwaves wrote:
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.
And that's a pain, isn't it?

I always solve this problem in one of the following ways:
1) Ask the user 3 inputfields, day/month/year, or whatever order is
appropriate in some country.
2) Make my own datestring for the database-insert, or make my own date for
PHP purposes, which is easy once you know what field is what.

Alernatively, if you want to offer your visitor ONE field for a date, just
make sure YOU parse it the way it should be before using.
eg:
[html]

format: d/m/y
<input type="text" name="somedate">

[php]
$dateparts = split("/",$_POST["somedate"]);
// now reasssemble any way you want: eg:
DateForPostgres = $dateparts[1]."/"$dateparts[0]."/".$dateparts[2];

(assuming Postgres is set to take m/d/y dates.)

Regards,
Erwin Moller


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 );
}


Apr 25 '06 #8
> 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.


Doesn't setlocale() sort this out? What locale is the server your using
running? I always thought strtotime() obeyed the current locale
settings, strftime() certainly does.

Apr 25 '06 #9
Erwin Moller wrote:

[typocorrection]

$DateForPostgres = $dateparts[1]."/".$dateparts[0]."/".$dateparts[2];

Apr 25 '06 #10
Toby Inkster wrote:
....
I'll let you work out the regular expressions for the conditionals
yourself. :-)

Thanks Toby, especially because that is the one thing I really suck at most.
Do you know the best way to learn regex? I have been looking on the net a
lot but never found a good tutorial.

Thanks again man!
Nicolaas

Apr 25 '06 #11
Mladen Gogala wrote:
On Tue, 25 Apr 2006 13:41:31 +1200, windandwaves wrote:
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.


Have you considered moving to Wyoming or Iowa? No waves there, but no
great white sharks, either. Alternatively, you can use PEAR::Date
class which contains different functions to format dates and time.

hmmm, i dont control the server ... so no Pear I think.
Apr 26 '06 #12
windandwaves wrote:
Mladen Gogala wrote:
On Tue, 25 Apr 2006 13:41:31 +1200, windandwaves wrote:
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.


Have you considered moving to Wyoming or Iowa? No waves there, but no
great white sharks, either. Alternatively, you can use PEAR::Date
class which contains different functions to format dates and time.


hmmm, i dont control the server ... so no Pear I think.


You can still download the PEAR libraries you require and put them in a
directory in your web tree (eg called 'pear') and then add that to your
include path. I've done this many times before for PEAR libraries that
a third party host does not have.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Apr 26 '06 #13
Chris Hope wrote:
......
You can still download the PEAR libraries you require and put them in
a directory in your web tree (eg called 'pear') and then add that to
your include path. I've done this many times before for PEAR
libraries that a third party host does not have.


HMMMM, that is way cool! Thanks for that. Superuseful!
Apr 26 '06 #14
On Tue, 25 Apr 2006 04:55:54 GMT, Mladen Gogala wrote:
On Tue, 25 Apr 2006 13:41:31 +1200, windandwaves wrote:
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.


Have you considered moving to Wyoming or Iowa? No waves there, but no
great white sharks, either. Alternatively, you can use PEAR::Date class
which contains different functions to format dates and time.


Aaah yes ... but are there any *sheep*???

Adam (also in NZ).
Apr 27 '06 #15
On 25 Apr 2006 01:13:41 -0700, fletch wrote:
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.


Doesn't setlocale() sort this out? What locale is the server your using
running? I always thought strtotime() obeyed the current locale
settings, strftime() certainly does.


Wouldn't you still have the problem of (let's say) US visitors
entering the date in the US format - regardless of the locale setting
on the server?

I tend to go for belt & braces - Erwin's 3-field solution. I just
create 3 (clearly labelled) drop-downs.

Adam.
Apr 27 '06 #16
windandwaves wrote:
Thanks Toby, especially because that is the one thing I really suck at most.
Do you know the best way to learn regex? I have been looking on the net a
lot but never found a good tutorial.


Google: "perlre".

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Apr 30 '06 #17
Toby Inkster wrote:
windandwaves wrote:
Thanks Toby, especially because that is the one thing I really suck
at most. Do you know the best way to learn regex? I have been
looking on the net a lot but never found a good tutorial.


Google: "perlre".


Will do!
Apr 30 '06 #18
> Wouldn't you still have the problem of (let's say) US visitors
entering the date in the US format - regardless of the locale setting
on the server?


Generally yes, but that was stated not to be a concern iirc

May 2 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pjotr Wedersteers | last post by:
I'm using strtotime to get the timestamp for midnight. I have a statistics script for my pagecounter that displays hits since midnight. I also display the number of hours and minutes passed since...
1
by: peabody | last post by:
I'm trying to use the strtotime() function to manage sessions. But I get the following <?php print(time() . " - " . strtotime("+1 hour")); ?> outputs: 1097380666 - 1097308800
5
by: Leif K-Brooks | last post by:
PHP has a very nice strtotime function (http://php.net/strtotime) which converts a date/time in virtually any format into a timestamp. Does Python have a similar function?
7
by: Rithish | last post by:
Hello. I noticed a strange thing while using strtotime() and date() functions in combination to generate from MySQL into a readable format. By default, the MySQL date field will be 0000-00-00...
6
by: Brian Kendig | last post by:
I'm working with dates in several formats including 'yyyy MMM dd', but strtotime doesn't recognize this format and returns FALSE. Is there a direct way to convert times from this format into...
5
by: cla | last post by:
I'm using this code on an application to track football schedules: ---- $season = '2005'; $basedate = strtotime('this friday', strtotime('31 August '.$season)); for($d=0;$d<=31;$d++) {...
5
by: Chris | last post by:
I am trying to output Monday of the current week i.e. if Monday is the 8th I want to display 'Monday 8th' for any date between Monday 8-14th. I would appreciate any help, the code below is...
3
by: Anthony Smith | last post by:
So, after doing some research I finally figured that php chokes on dates past year 2525. In this web service that I use they often use 2525-05-31 as the expiration date. I want to determine how I...
9
by: Erwin Moller | last post by:
Hi group, I have been using strtotime a lot in my code. I wonder if I made a mistake in my thinking. :-/ Here follows a stripped down example. consider some dates: $date1 = "2008-02-23";...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.