RE: Re: String to Date and Date to String Functions?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm



On 25 January 2005 00:25, Ben Edwards wrote:

> On Mon, 24 Jan 2005 20:15:15 -0000, Ford, Mike
> <M.Ford@xxxxxxxxxxxxxx> wrote:
> > To view the terms under which this email is distributed,
> please go to http://disclaimer.leedsmet.ac.uk/email.htm
> > 
> > On 24 January 2005 19:01, Ben Edwards wrote:
> > 
> > > On Mon, 24 Jan 2005 12:58:52 -0500, hitek@xxxxxxx
> > > <hitek@xxxxxxx> wrote:
> > > > How about the strtotime() function?
> > > > http://us4.php.net/manual/en/function.strtotime.php
> > > 
> > > No good, it douse not take a format string.  What if I wanted to
> > > convert the timestamp to 'DD-MM-YYY', display it on a form,
> > > validate it, and then convert it back to a timestamp.
> > 
> > Well, if you're going to validate it, you'll have to figure out
> > which bits are the day, month and year -- having done that, you'll
> > have them as three separate bits of information, so you can use
> > mktime() on them, or glue them back together to feed to strtotime()
> > in one of the various formats it recognizes.  Seems to me a format
> > string is pretty irrelevant in all of this.
> 
> Different users have can have the date displayed in there local form.

Which means you've got to know what format any submitted date should be in,
which means you've got to know which bits are day, month and year.

> And no, I dont have to split the date up, the date() function should
> be able to be used to validate the date.  I asume it returns null, or
> something sensable, if the date is invalid.

date() won't do this, it's the function used to display a given timestamp in
a human-readable format.  Functions like mktime() accept apparently invalid
values so as to allow for easy date arithmetic (date('d-M-Y', mktime(0, 0,
0, 1, 32, 2005)) will give you '01-Feb-2005', for example; strtotime() does
something similar).  The only built-in function that does the checking you
are after is checkdate(), and that also wants day, month and year as three
integers.

So yes, you do have to split the input date up.

>   Anyway as the date format
> is user defined I need both functions.

No, you don't *need* them.  You may prefer to have the function to decode
according to a format string, but you don't need it -- it's pretty trivial
to do the task without it.

(    $aDate = explode('-', $inDate);	// or preg_split("#[/.-]#", $inDate)
     if (checkdate($aDate[1], $aDate[0], $aDate[2])):
         $timestamp = mkdate(12, 0, 0, $aDate[1], $aDate[0], $aDate[2]);
     else:
         $timestamp = NULL;
     endif;
)

> In this case it is a back end administration form as the (power) user
> does not want to have to fiddle about with drop downs.  In fact they
> can use / or - or . as a seperater.  The split function can handle
> that. 

Well, but then you're splitting the date up into its constituent components
anyway -- so what's all the fuss about???

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: m.ford@xxxxxxxxxxxxxx
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux