By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,713 Members | 1,531 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,713 IT Pros & Developers. It's quick & easy.

mktime question

P: n/a
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.
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #2

P: n/a
> 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...
Jul 17 '05 #3

P: n/a
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
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.