Jump to content

convert date/time


AV1611

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.