Jump to content

Archived

This topic is now archived and is closed to further replies.

AV1611

convert date/time

Recommended Posts

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 stamp


2006.07.08 01:41:10

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
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";
      }
?>

Share this post


Link to post
Share on other sites
Ok, here is the new code:[code]<?php
//...
$timestamp = "2006.07.08 01:41:10"; // i suppose this comes from somewere else
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);
}

$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.

Share this post


Link to post
Share on other sites
You totally get to be my hero for the rest of the day

;D

Share this post


Link to post
Share on other sites
Heh 8)

Note that this will only work if the date is ALWAYS in that format and if it always have leading zeroes.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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
}
}
?>

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.