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 Quote 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; ?> Quote 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' Quote 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 ... Quote 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. Quote 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. Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.