chrisfane Posted August 31, 2007 Share Posted August 31, 2007 im not sure which function i should use to trim a number of charactors from the end of my string. i already have a timestamp i nthe form of ... 2007-08-26 12:35:29 in my database, i need to split this into a separate time and date, which unfortunatly cant be done at the time of writing. to do this i propose trimming after ten charators, or anything after and including the space, and then on a second pass trimming everything before and including the space. my question is... Which function would should i use to do this ? everything i have previously found can trim by charactor, but not by number. Thanks Link to comment https://forums.phpfreaks.com/topic/67450-solved-trimming-text-from-and-to/ Share on other sites More sharing options...
hostfreak Posted August 31, 2007 Share Posted August 31, 2007 I would use <a href="http://www.php.net/explode">Explode</a> Example: <?php $timestamp = "2007-08-26 12:35:29"; $arr = explode(" ", $timestamp); $Date = $arr[0]; $Time = $arr[1]; echo $Date . '<br />' . $Time; ?> Link to comment https://forums.phpfreaks.com/topic/67450-solved-trimming-text-from-and-to/#findComment-338598 Share on other sites More sharing options...
Fadion Posted August 31, 2007 Share Posted August 31, 2007 $date = '2007-08-26 12:35:29'; echo substr($date, 0, 10); //prints '2007-08-26' echo substr($date, 10) //prints '12:35:29' or $date = '2007-08-26 12:35:29'; $expl = explode(' ', $date); echo $expl[0]; //prints '2007-08-26' echo $expl[1]; //prints '12:35:29' Link to comment https://forums.phpfreaks.com/topic/67450-solved-trimming-text-from-and-to/#findComment-338599 Share on other sites More sharing options...
akitchin Posted August 31, 2007 Share Posted August 31, 2007 alternatively, assuming you're pulling a timestamp like this from a MySQL database, you can use their functions: SELECT DATE(timestamp_col) AS date_portion, TIME(timestamp_col) AS time_portion FROM ... Link to comment https://forums.phpfreaks.com/topic/67450-solved-trimming-text-from-and-to/#findComment-338600 Share on other sites More sharing options...
Daniel0 Posted August 31, 2007 Share Posted August 31, 2007 You could also use strtotime() to turn it into a UNIX timestamp and then use date() to format it yourself. Link to comment https://forums.phpfreaks.com/topic/67450-solved-trimming-text-from-and-to/#findComment-338602 Share on other sites More sharing options...
chrisfane Posted August 31, 2007 Author Share Posted August 31, 2007 WOW, that was quick. Thankyou all very much for your help. Link to comment https://forums.phpfreaks.com/topic/67450-solved-trimming-text-from-and-to/#findComment-338606 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.