strtotime() problems...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

strtotime() problems...

Post by dhrosti »

Im having trouble trying to convert DD-MM-YYYY to a timestamp using strtotime().

Is there a way of telling the function what the date format is that im giving to it?

I think its getting confused as to wether im giving it dd-mm-yyyy or mm-dd-yyyy.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

What's the source of this DD-MM-YYYY datestring?
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post by dhrosti »

Its a user input. I though it would be more straight forward for the user to input the date in the format they're used to. Im currently looking at explode() and mktime() which looks like it could work.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

I'm sure explode and mktime will help you as I did it once when I didn't use any library of date. There is also an alternative. You can use PEAR:DATE library which is quiet easy to use.
Last edited by dude81 on Mon Mar 05, 2007 6:16 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I'm used to dd.mm.yyyy or mm/dd/yyyy or yyyy-mm-dd and so is strtotime.
Who's used to dd-mm-yyyy? (Real question, I honestly don't know)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

mm-dd-yyyy is the UK/Cdn standard I believe, which is why strtotime is getting confused. Maybe you could preg_replace() it on the way in?
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post by dhrosti »

Code: Select all

// For example, 23rd Jan 2006...
$date = "23-01-2006";
$comp = explode("-", $date);
$time = mktime(12, 0, 0, $comp[1], $comp[0], $comp[2]);
It works, but is this not a good way of doing it?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I still believe it's unnecessary ;) Who's used to dd-mm-yyyy?
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post by dhrosti »

Not sure really, the boss just decided to do it like that.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd suggest using a date picker over attempting to handle user input such as that. There are just too many combinations otherwise.
Post Reply