cooldude832 Posted November 29, 2007 Share Posted November 29, 2007 I have a string that looks like (a series of them) 1 Sep 2007 21:06:11 2 Sep 2007 13:45:31 2 Sep 2007 13:45:31 3 Sep 2007 01:40:42 and I want to go strto time on it any ideas its return -1 right now so I know somethings wrong. Link to comment https://forums.phpfreaks.com/topic/79353-solved-strtotime-question/ Share on other sites More sharing options...
wsantos Posted November 29, 2007 Share Posted November 29, 2007 try this if this would help. <?php $str = 'Not Good'; // previous to PHP 5.1.0 you would compare with -1, instead of false if (($timestamp = strtotime($str)) === false) { echo "The string ($str) is bogus"; } else { echo "$str == " . date('l dS \o\f F Y h:i:s A', $timestamp); } ?> http://php.net/strtotime Link to comment https://forums.phpfreaks.com/topic/79353-solved-strtotime-question/#findComment-401697 Share on other sites More sharing options...
cooldude832 Posted November 29, 2007 Author Share Posted November 29, 2007 not really, it just says if its a junk string, its not a junk string I need to make it readable by strtotime Link to comment https://forums.phpfreaks.com/topic/79353-solved-strtotime-question/#findComment-401698 Share on other sites More sharing options...
cooldude832 Posted November 29, 2007 Author Share Posted November 29, 2007 this made it happy finally <?php $time2 = explode(" ",$time[0]); switch($time2[2]){ case Jan: $month = 1; break; case Feb: $month = 2; break; case Mar: $month = 3; break; case Apr: $month = 4; break; case May: $month = 5; break; case Jun: $month = 6; break; case Jul: $month = 7; break; case Aug: $month = 8; break; case Sep: $month = 9; break; case Oct: $month = 10; break; case Nov: $month = 11; break; case Dec: $month = 12; break; default: $month = 1; } $time3 = $time2[3]."/".$month."/".$time2[1]." ".$time2[4]; $time4 = strtotime($time3); ?> Link to comment https://forums.phpfreaks.com/topic/79353-solved-strtotime-question/#findComment-401708 Share on other sites More sharing options...
teng84 Posted November 29, 2007 Share Posted November 29, 2007 i believe dont need the switch statement you can manipulate that using mktime/date Link to comment https://forums.phpfreaks.com/topic/79353-solved-strtotime-question/#findComment-401709 Share on other sites More sharing options...
cooldude832 Posted November 29, 2007 Author Share Posted November 29, 2007 I thought so also, but it didn't like the 3 character months Link to comment https://forums.phpfreaks.com/topic/79353-solved-strtotime-question/#findComment-401710 Share on other sites More sharing options...
wsantos Posted November 29, 2007 Share Posted November 29, 2007 Correct me if I'm wrong...Ain't you getting the year here rather than the month. switch($time2[2]){ Link to comment https://forums.phpfreaks.com/topic/79353-solved-strtotime-question/#findComment-401712 Share on other sites More sharing options...
cooldude832 Posted November 29, 2007 Author Share Posted November 29, 2007 I ran a print_r on it and the 0 on the array is actually blank (could clean it up but what ever), so I just arranged accord to the print_r results Link to comment https://forums.phpfreaks.com/topic/79353-solved-strtotime-question/#findComment-401714 Share on other sites More sharing options...
kenrbnsn Posted November 29, 2007 Share Posted November 29, 2007 I did a quick test using: <?php $strs = array('1 Sep 2007 21:06:11','2 Sep 2007 13:45:31','2 Sep 2007 13:45:31','3 Sep 2007 01:40:42'); foreach($strs as $dte) echo date('F j, Y g:i A',strtotime($dte)).'<br>'; ?> And got the following printed: September 1, 2007 9:06 PM September 2, 2007 1:45 PM September 2, 2007 1:45 PM September 3, 2007 1:40 AM which is correct. Please post the code that gave you the incorrect results. Ken Link to comment https://forums.phpfreaks.com/topic/79353-solved-strtotime-question/#findComment-401720 Share on other sites More sharing options...
teng84 Posted November 29, 2007 Share Posted November 29, 2007 echo date('d-m-Y',strtotime('1 Jan 2007 21:06:11'));//01-01-2007 i get the number format of month... Link to comment https://forums.phpfreaks.com/topic/79353-solved-strtotime-question/#findComment-401722 Share on other sites More sharing options...
cooldude832 Posted November 29, 2007 Author Share Posted November 29, 2007 I think it might be spacing issues on it, but if I know strtotime will take a string formatted D m Y H:M:S then I just need to fix up the spacing on it Link to comment https://forums.phpfreaks.com/topic/79353-solved-strtotime-question/#findComment-401723 Share on other sites More sharing options...
teng84 Posted November 29, 2007 Share Posted November 29, 2007 to avoid that explode issue trim first your string and again no need to use that explode and switch its a waste of time and consume lot of lines Link to comment https://forums.phpfreaks.com/topic/79353-solved-strtotime-question/#findComment-401725 Share on other sites More sharing options...
wsantos Posted November 29, 2007 Share Posted November 29, 2007 to avoid that explode issue trim first your string and again no need to use that explode and switch its a waste of time and consume lot of lines I agree on the concept of cleaning up the data before and after. Link to comment https://forums.phpfreaks.com/topic/79353-solved-strtotime-question/#findComment-401730 Share on other sites More sharing options...
cooldude832 Posted November 29, 2007 Author Share Posted November 29, 2007 starting with Received: (qmail 6265 invoked by uid 399); 29 Nov 2007 02:55:45 -0000 Received: from rv-out-0910.google.com (209.85.198.185) by manhattan.worldispnetwork.com with SMTP; 29 Nov 2007 02:55:45 -0000 Received: by rv-out-0910.google.com with SMTP id k15so1441365rvb for <[email protected]>; Wed, 28 Nov 2007 18:55:44 -0800 (PST) Received: by 10.140.135.20 with SMTP id i20mr3017628rvd.1196304944936; Wed, 28 Nov 2007 18:55:44 -0800 (PST) Received: by 10.140.202.18 with HTTP; Wed, 28 Nov 2007 18:55:44 -0800 (PST) <?php $time = explode(";",$value['Header']['Received']); $time = explode(" -",$time[1]); $time_a = trim($time[0]); $time_a = strtotime($time_a); echo "Date: ".date("Y/m/d",$time_a)."<br />"; } ?> outputs: Date: 2007/09/01 Link to comment https://forums.phpfreaks.com/topic/79353-solved-strtotime-question/#findComment-401735 Share on other sites More sharing options...
teng84 Posted November 29, 2007 Share Posted November 29, 2007 yah.. junk the switch and explode LOL Link to comment https://forums.phpfreaks.com/topic/79353-solved-strtotime-question/#findComment-401741 Share on other sites More sharing options...
cooldude832 Posted November 29, 2007 Author Share Posted November 29, 2007 yeah trim is your best friend, i didn't think strtotime was that powerful Link to comment https://forums.phpfreaks.com/topic/79353-solved-strtotime-question/#findComment-401744 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.