Jump to content


Photo

convert date/time


  • Please log in to reply
8 replies to this topic

#1 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 08 July 2006 - 02:01 PM

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

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 08 July 2006 - 02:05 PM

<?php
//...
if($timestamp+(3600*4) <= time())
{
	// within four hours
}
else {
	// not within four hours
}
?>

This way you can check if the timestamp is within four hours.

#3 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 08 July 2006 - 03:04 PM

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";
      }
?>


#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 08 July 2006 - 03:28 PM

Ok, here is the new 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";
}
?>

There was an error in the old code, so don't use it, use this.

#5 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 08 July 2006 - 03:40 PM

You totally get to be my hero for the rest of the day

;D

#6 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 08 July 2006 - 03:41 PM

Heh 8)

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

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 July 2006 - 04:14 PM

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:
<?php
$test_dt = '2006.07.08 01:41:10';
$timestamp = strtotime(str_replace('.','/',$test_dt));
echo $timestamp;
?>

Ken

#8 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 08 July 2006 - 05:09 PM

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

#9 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 08 July 2006 - 11:35 PM

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
}
}
?>




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users