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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
> 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... |
|
|||
|
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 |