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

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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.