Jump to content

What is the script to take string appart?


pioneerx01

Recommended Posts

I am trying to separate a values into independent components. In my case it is a time stamp such as 08-22-2011 18:02. I have seen the code before but I can not remember where or what it is called. Basically I define that this string should be exploded with values being between -, -, ,: and reminders are $day, $month, $year, $hour, $minute.

 

If you can point me in the correct direction I would appreciate it.

 

Thanks

Link to comment
Share on other sites

Two ways off the top of my head:

<?php
//using date function. (not recommended, cause I think it is dirty).
$timestamp = '2011-08-22 18:02:00'; //notice the timestamp change from the format provided.
$time = strtotime($timestamp);
$month = date('m',$time);
$day = date('d',$time);
$year = date('Y',$time);
$hour = date('H',$time);
$minute = date('i',$time);
$second = date('s',$time);
echo 'Month: ' . $month . '<br />Day: ' . $day . '<br />Year: ' . $year . '<br />Hour: ' . $hour . '<br />Minute: ' . $minute . '<br />Second: ' . $second . '<hr />';

//using explode
$timestamp = '08-22-2011 18:02:00'; 
$split = explode(' ',$timestamp);
list($month, $day, $year)  = explode('-',$split[0]);
list($hour, $minute, $second) = explode(':',$split[1]);

echo 'Month: ' . $month . '<br />Day: ' . $day . '<br />Year: ' . $year . '<br />Hour: ' . $hour . '<br />Minute: ' . $minute . '<br />Second: ' . $second;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.