AV1611 Posted July 8, 2006 Share Posted July 8, 2006 I have a script that pulls out a text string that is actually a date time stamp from a program. I need to convert it to a number so I can write a clause that does something only if the data/time is within the past 4 hours.can you help?here is the date time stamp2006.07.08 01:41:10 Quote Link to comment https://forums.phpfreaks.com/topic/14027-convert-datetime/ Share on other sites More sharing options...
Daniel0 Posted July 8, 2006 Share Posted July 8, 2006 [code]<?php//...if($timestamp+(3600*4) <= time()){ // within four hours}else { // not within four hours}?>[/code]This way you can check if the timestamp is within four hours. Quote Link to comment https://forums.phpfreaks.com/topic/14027-convert-datetime/#findComment-54777 Share on other sites More sharing options...
AV1611 Posted July 8, 2006 Author Share Posted July 8, 2006 thank you, but your script does not convert the text string timestamp into a usable format... that is what I need to do...Here is what I have so far...<?php$timestamp="2006.07.08 01:41:10"; //mock up of what the program has to work witth//need to convert $timestamp before the clause but don't know how...if($timestamp+(3600*4) <= time()) { ECHO " within four hours"; }else { echo " not within four hours"; }?> Quote Link to comment https://forums.phpfreaks.com/topic/14027-convert-datetime/#findComment-54798 Share on other sites More sharing options...
Daniel0 Posted July 8, 2006 Share Posted July 8, 2006 Ok, here is the new code:[code]<?php//...$timestamp = "2006.07.08 01:41:10"; // i suppose this comes from somewere elsefunction convert($t){ $year = substr($t,0,4); $month = substr($t,5,2); $day = substr($t,8,2); $hour = substr($t,11,2); $minute = substr($t,14,2); $second = substr($t,17,2); return mktime($hour,$minute,$second,$month,$day,$year);}$timestamp = convert($timestamp);if($timestamp+(3600*4) >= time()){ // within four hours echo "Within four hours old";}else { // not within four hours echo "Not within four hours old";}?>[/code]There was an error in the old code, so don't use it, use this. Quote Link to comment https://forums.phpfreaks.com/topic/14027-convert-datetime/#findComment-54804 Share on other sites More sharing options...
AV1611 Posted July 8, 2006 Author Share Posted July 8, 2006 You totally get to be my hero for the rest of the day ;D Quote Link to comment https://forums.phpfreaks.com/topic/14027-convert-datetime/#findComment-54805 Share on other sites More sharing options...
Daniel0 Posted July 8, 2006 Share Posted July 8, 2006 Heh 8)Note that this will only work if the date is ALWAYS in that format and if it always have leading zeroes. Quote Link to comment https://forums.phpfreaks.com/topic/14027-convert-datetime/#findComment-54807 Share on other sites More sharing options...
kenrbnsn Posted July 8, 2006 Share Posted July 8, 2006 Another method that doesn't break if the there are no leading zeros or if there is more than one space between the date and the time:[code]<?php$test_dt = '2006.07.08 01:41:10';$timestamp = strtotime(str_replace('.','/',$test_dt));echo $timestamp;?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/14027-convert-datetime/#findComment-54818 Share on other sites More sharing options...
Daniel0 Posted July 8, 2006 Share Posted July 8, 2006 Yeah, I know that one. But I just thought if it were able to see in that example if 07 or 08 is the month... Quote Link to comment https://forums.phpfreaks.com/topic/14027-convert-datetime/#findComment-54835 Share on other sites More sharing options...
AV1611 Posted July 8, 2006 Author Share Posted July 8, 2006 Here is what I ended up with:<?php$tnow=time();//$tminus4=strtotime("-4 hours");$tminus4=$tnow-7200;function convert($t) { $year = substr($t,0,4); $month = substr($t,5,2); $day = substr($t,8,2); $hour = substr($t,11,2); $minute = substr($t,14,2); $second = substr($t,17,2); return mktime($hour,$minute,$second,$month,$day,$year); }// Get a file into an array. In this example we'll go through HTTP to get// the HTML source of a URL.$lines = file('http://xxx/pbsvss.htm');// Loop through our array, show HTML source as HTML source; and line numbers too.foreach ($lines as $line_num => $line) { $line=str_replace('<a href=','<a href=http://xxx/',$line); $line=str_replace('<p> <a','<a',$line); $line=str_replace('a> "','a>|||',$line); $line=str_replace('_blank>','_blank>',$line); $line=str_replace('" (W) ','|||',$line); $line=str_replace('" ( ) ','|||',$line); $line=str_replace(') [',')|||[',$line); $line=str_replace(']\n','|||',$line); $line=str_replace('GUID=','',$line); $col=explode("|||",$line); $image=$col[0]; $image=str_replace('<a href=','<img src=',$image); $image=str_replace('</a>',' ',$image); $image=str_replace('.htm','.png',$image); $image=str_replace('target=_blank>','><br/>',$image); $datetime=str_replace('[','',$col[3]); $datetime=str_replace(']','',$datetime); $timestamp = convert($datetime); $test=$timestamp-$tminus4; if($test >= 0) { echo "<center>"; echo $image; echo "<table border ='1'><tr><td>LINK: </td><td>".$col[0]."</td></tr><tr><td>NAME: </td><td>"; echo $col[1]."</td></tr><tr><td>GUID: </td><td>"; echo $col[2]."</td></tr><tr><td>TIMESTAMP: </td><td>"; echo $datetime."</td></tr></table><br/></center>"; } else { // not within four hours } }?> Quote Link to comment https://forums.phpfreaks.com/topic/14027-convert-datetime/#findComment-54978 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.