mktime question

This is a discussion on mktime question within the PHP Language forums, part of the PHP Programming Forums category; I understand that mktime will convert a date to a unix timestamp: mktime(13, 42, 0, 10, 5, 2004) but ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-08-2004
deko
 
Posts: n/a
Default mktime question

I understand that mktime will convert a date to a unix timestamp:

mktime(13, 42, 0, 10, 5, 2004)

but if my dates are this format:

Oct 05 2004 01:42 pm

how do I convert each element to a number? Is there a function for this?
Do I need to use an array? Switch statements?

Thanks in advance.


Reply With Quote
  #2 (permalink)  
Old 10-08-2004
Shawn Wilson
 
Posts: n/a
Default Re: mktime question

deko wrote:
>
> I understand that mktime will convert a date to a unix timestamp:
>
> mktime(13, 42, 0, 10, 5, 2004)
>
> but if my dates are this format:
>
> Oct 05 2004 01:42 pm
>
> how do I convert each element to a number? Is there a function for this?
> Do I need to use an array? Switch statements?


http://ca3.php.net/manual/en/function.strtotime.php
http://www.gnu.org/software/tar/manu..._7.html#SEC110

Not sure if your format conforms to an accepted one or not. If it doesn't,
you'll likely need a preg_replace() with a regex (something like "/^([a-z]+)
(\d{2}) (\d{4}) (\d{2}):(\d{2}) (am|pm)$/i" ) <-- UNTESTED.

Shawn
--
Shawn Wilson
shawn@glassgiant.com
http://www.glassgiant.com
Reply With Quote
  #3 (permalink)  
Old 10-09-2004
deko
 
Posts: n/a
Default Re: mktime question

> Not sure if your format conforms to an accepted one or not. If it
doesn't,
> you'll likely need a preg_replace() with a regex (something like

"/^([a-z]+)
> (\d{2}) (\d{4}) (\d{2}):(\d{2}) (am|pm)$/i" ) <-- UNTESTED.


It seems getting "Oct 05 2004 01:42 pm" into "13, 42, 0, 10, 5, 2004" is the
challenge. I was thinking I could explode each string into an array and
then use a switch statement for translating the month, and add 12 to the
first side of the ":" in the time (another array) if "pm", and then just put
the array back together separated by commas. will have to do some
testing...


Reply With Quote
  #4 (permalink)  
Old 10-09-2004
Pedro Graca
 
Posts: n/a
Default Re: mktime question

deko wrote:
> It seems getting "Oct 05 2004 01:42 pm" into "13, 42, 0, 10, 5, 2004" is the
> challenge.



$ cat time.php
<?php
$in = "Oct 05 2004 01:42 pm";
$out = strtotime($in);

if ($out == NULL) echo "unable to convert\n";
else {
echo "\"$in\" converts to ",
date('G, i', $out), ", ",
1*date('s', $out), ", ",
date('n, j, Y', $out), "\n";
}
?>

$ php time.php
"Oct 05 2004 01:42 pm" converts to 13, 42, 0, 10, 5, 2004



--
================== My mail address is spam-trapped ==================
To: "My Name" <myaddress> | messages without these three
Content-Type: text/plain | conditions _will_ be sent to
size < 10K | /dev/null
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

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




All times are GMT +1. The time now is 07:39 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0