niche Posted March 20, 2012 Share Posted March 20, 2012 is there a function that turns 2012-03-19 20:41:28 into 28,41,20,19,03,2012 so mktime() can be applied? Else, the only way I can think of is exploding the timestamp into its parts. Link to comment Share on other sites More sharing options...
astralaaron Posted March 20, 2012 Share Posted March 20, 2012 that was my first thought as well, exploding EDIT: I did this to get my brain going..just woke up I don't know if it is the best way or not but it works <?phpfunction alterDateTime($string){$parts = explode(' ',$string);$date = $parts[0];$time = $parts[1];$time_parts = explode(':',$time);$date_parts = explode('-',$date);$new_string = "";for($i=sizeOf($time_parts)-1; $i >= 0; $i--) $new_string .= $time_parts[$i] . ',';for($i=sizeOf($date_parts)-1; $i >= 0; $i--) $new_string .= ($i < 1) ? $date_parts[$i] : ($date_parts[$i] . ',') ;return $new_string;}$dateTimeString = '2012-03-19 20:41:28';echo $dateTimeString . '<br />';$test = alterDateTime($dateTimeString);echo $test;?> Link to comment Share on other sites More sharing options...
niche Posted March 20, 2012 Author Share Posted March 20, 2012 Nice work astralaaron! Thank-you. Though do you know why mktime() throws a notice when it acts on a variable? Here's the notice I get: Notice: A non well formed numeric value encountered in E:\wamp\www\test_120319.php on line 21. Please notice line 22 doesn't produce a notice. code line20: $temp = "28,41,20,19,03,2012";line21: echo mktime($temp) . '<br />';line22: mktime(28,41,20,19,03,2012) . '<br />'; Link to comment Share on other sites More sharing options...
niche Posted March 20, 2012 Author Share Posted March 20, 2012 Turns out mktime() won't accept a string, but will accept this: mktime($sec,$min,$hr,$day,$mo,$year). I'll just tweak astralaaron's function a little bit. Thanks again astralaaron. Link to comment Share on other sites More sharing options...
dsonesuk Posted March 20, 2012 Share Posted March 20, 2012 strtotime() <?php$setdate="2012-03-19 20:41:28"; $convert = strtotime($setdate); echo date("s,i,H,d,m,Y",$convert); ?> well it looks like this won't work either, mind you, you do hours, sec the work way round for mktime() Link to comment Share on other sites More sharing options...
niche Posted March 20, 2012 Author Share Posted March 20, 2012 Thanks dsonesuk. I tried using strtotime() with strptime(), but couldn't hack it. $setdate="2012-03-19 20:41:28";$convert = strtotime($setdate);echo date("s,i,H,d,m,Y",$convert) . '<br/>';$year = strptime($convert,%Y);echo $year; Produced no joy. How does strtotime() and strptime() work together to produce $year? Link to comment Share on other sites More sharing options...
dsonesuk Posted March 20, 2012 Share Posted March 20, 2012 ??? they don't The strptime() function parses a time/date generated with strftime(). http://www.w3schools.com/php/func_date_strptime.asp this seems to work $setdate="2012-03-19 20:41:28";$convert = strtotime($setdate);$hour = date("H",$convert);$min = date("i",$convert);$sec = date("s",$convert);$day = date("d",$convert);$mon = date("n",$convert);$year = date("Y", $convert);echo mktime($hour,$min,$sec,$mon,$day,$year); Link to comment Share on other sites More sharing options...
niche Posted March 20, 2012 Author Share Posted March 20, 2012 Thanks dsonesuk. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.