首页 > 编程技术 > php

php 判断文件修改时间与日期函数

发布时间:2016-11-25 15:58

此脚本时,页面最后修订和产出作为二十分钟前,或XX天前的日期...或者即使您不更新太多 - 二十周前显示!

//File Name
$last_modified = filemtime("FILE.php");
 
{
$timediff = time() - $last_modified;
 
if ($timediff < 3600)
{
if ($timediff < 120)
{
$returndate = "1 minute ago.";
}
else
{
$returndate = intval($timediff / 60) . " minutes ago.";
}
}
else if ($timediff < 7200)
{
$returndate = "1 hour ago.";
}
else if ($timediff < 86400)
{
$returndate = intval($timediff / 3600) . " hours ago.";
}
else if ($timediff < 172800)
{
$returndate = "1 day ago.";
}
else if ($timediff < 604800)
{
$returndate = intval($timediff / 86400) . " days ago.";
}
 
else if ($timediff < 1209600)
{
$returndate = "1 week ago.";
}
else if ($timediff < 3024000)
{
$returndate = intval($timediff / 604900) . " weeks ago.";
}
else
{
$returndate = @date('n-j-Y', $timestamp);
if($type=="fulldate")
{
$returndate = @date('n-j-y, H:i', $timestamp);
 
}
 
else if ($type=="time")
{
 
$returndate = @date('H:i', $timestamp);
 
}
 
}
//Display It
print("Last Modified: ");
print($returndate);
 
}
?>

它应该很容易理解。

$ last_modified = filemtime(“FILE.php”); - 更改FILE.php到文件您要连接。

现在我将告诉你如何看到有多少用户在浏览您的网站。有许多方法来显示它,但我试图保持它简单,干净。

首先,我们必须创建两个表,您的数据库教程。

CREATE TABLE `uonline` (
  `session` varchar(100) NOT NULL,
  `time` int(5) NOT NULL default '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

这将创建两个表。一个是会议和其他的是时间。他们将我们的信息存储。

现在是PHP的时间。让我们一起创造我们的主要变数

<?php教程
session_start();
$ses = session_id();
$time = time();
$timech=$time-300;
session_start(); - This wills start a session
$ses = session_id(); - This will get our session's ID
$time = time(); - This will get the time
$timech=$time-300; - This will set our time to 5min

Now we have to connect to the database:


// Declare SQL login info
$host = "localhost";
$username = "";
$password = "";
$dbname = "";
 
// Now we are going to connect to our database
mysql教程_connect("$host", "$username", "$password")or die("<font style='color:red'><b>Can not connect:</b> ".mysql_error()."</font>");
mysql_select_db("$db_name")or die("<font style='color:red'><b>Can not select the database:</b> ".mysql_error()."</font>");

$host = "localhost"; - Your SQL's host name, usually it's "localhost"
$username = ""; - Your MySQL username
$password = ""; - Your MySQL password
$dbname = ""; - Your database's name

Now we have to look for existing sessions and get the number of sessions.


$result = mysql_query("SELECT * FROM uonline WHERE session='$ses'");
$num = mysql_num_rows($result); 

$result = mysql_query("SELECT * FROM uonline WHERE session='$ses'"); - This will select all info from session column where it's value is "$ses"
$num = mysql_num_rows($result); - It will tell us how many active records are in session column


if($num == "0"){
$result1 = mysql_query("INSERT INTO uonline (session, time)VALUES('$ses', '$time')");
}else{
$result2 = mysql_query("UPDATE uonline SET time='$time' WHERE session = '$ses'");
}

if($num=="0"){ - If there's no records in session column then we must insert some records
$result1 = mysql_query("INSERT INTO uonline (session, time)VALUES('$ses', '$time')"); - Inserts session's ID and time to database
}else{ - But if there was more records than 0, let's update their records
$result2 = mysql_query("UPDATE uonline SET time='$time' WHERE session = '$ses'"); - Updates existing records
} - Ends If statement

Now let's find our info again from the columns:


$result3 = mysql_query("SELECT * FROM uonline");

$result3 = mysql_query("SELECT * FROM uonline"); - This will get all info from uonline table

It's time for showing how many users are looking your site:


$usersonline = mysql_num_rows($result3);
echo "There are: <b>".$usersonline."</b> users online";

$usersonline = mysql_num_rows($result3); - It will get the number of records in columns
echo "There are: <b>".$usersonline."</b> users online"; - This will show how many users are on your site

When the users have left, you must delete their records from database.


mysql_query("DELETE FROM uonline WHERE time<$timech");
?>

mysql_query("DELETE FROM uonline WHERE time<$timech"); - Deletes records from database when 5min has been thru.

And here's the full code:


<?php
session_start();
$ses = session_id();
$time = time();
$timech=$time-300; 
 
$host = "localhost";
$username = "";
$password = "";
$dbname = "";
 
mysql_connect("$host", "$username", "$password")or die("<font style='color:red'><b>Can not connect:</b> ".mysql_error()."</font>");
mysql_select_db("$db_name")or die("<font style='color:red'><b>Can not select the database:</b> ".mysql_error()."</font>");  
 
$result = mysql_query("SELECT * FROM uonline WHERE session='$ses'");
$num = mysql_num_rows($result); 
 
if($num == "0"){
$result1 = mysql_query("INSERT INTO uonline (session, time)VALUES('$ses', '$time')");
}else{
$result2 = mysql_query("UPDATE uonline SET time='$time' WHERE session = '$ses'");

 
$result3 = mysql_query("SELECT * FROM uonline"); 
 
$usersonline = mysql_num_rows($result3);
echo "There are: <b>".$usersonline."</b> users online";  
 
mysql_query("DELETE FROM uonline WHERE time<$timech");
?>

确定了PHP脚本所需的时间执行正确的一微秒。

插入在页面顶部的代码:

<?php 
          $mtime = microtime(); 
          $mtime = explode(' ', $mtime); 
          $mtime = $mtime[1] + $mtime[0]; 
          $starttime = $mtime; 
?>
然后添加在页面底部的以下内容:

<?php 
          $mtime = microtime(); 
          $mtime = explode(" ", $mtime); 
          $mtime = $mtime[1] + $mtime[0]; 
          $endtime = $mtime; 
          $totaltime = ($endtime - $starttime); 
          echo 'This page was created in ' .$totaltime. ' seconds.'; 
?>

PHP的microtime中()函数返回当前的Unix时间戳微秒。

 

有时,您可能希望让无条件循环的开始,并允许括号内的语句来决定何时退出循环。

有两个特殊的语句可用在循环使用:中断和继续。

break语句终止或For循环的同时,继续执行现行的代码如下循环后(如有)。或者,

你可以把一个数字后,折价关键字,说明如何循环结构的多层次,以摆脱。这样,埋

藏在一份声明中深层嵌套的循环可以打破最外层循环。

<?php
echo "<p><b>Example of using the Break statement:</b></p>";

for ($i=0; $i<=10; $i++) {
   if ($i==3){break;}
   echo "The number is ".$i;
   echo "<br />";
}

echo "<p><b>One more example of using the Break statement:</b><p>";

$i = 0;
$j = 0;

while ($i < 10) {
  while ($j < 10) {
    if ($j == 5) {break 2;} // breaks out of two while loops教程
    $j++;
  }
  $i++;
}

echo "The first number is ".$i."<br />";
echo "The second number is ".$j."<br />";
?>

continue语句终止了在语句块执行一或For循环的同时,继续对下一个迭代循环的执行

<?php
echo "<p><b>Example of using the Continue statement:</b><p>";

for ($i=0; $i<=10; $i++) {
   if (i==3){continue;}
   echo "The number is ".$i;
   echo "<br />";
}
?>

foreach循环是一种变异的For循环,并允许您遍历数组中的元素。有两个不同版本的

foreach循环。 foreach循环的语法如下:

foreach (array as value)
{
   code to be executed;
}
   
foreach (array as key => value)
{
   code to be executed;
}
下面的例子演示了foreach循环,将打印给定数组的值:

<?php
$email = array('john.smith@example.com', 'alex@example.com');
foreach ($email as $value) {
   echo "Processing ".$value."<br />";
}
?>

PHP的执行每个元素美元,反过来电子邮件循环体一次,美元值设置为当前元素。元素

处理其国内秩序。循环继续,直到达到foreach循环的最后一个元素或上给定数组的约

束。

<?php
$person = array('name' => 'Andrew', 'age' => 21, 'address' => '77, Lincoln

st.');
foreach ($person as $key => $value) {
   echo $key." is ".$value."<br />";
}
?>

在这种情况下,对每个元素的关键是放置在$键和相应的值是$的重视。

在foreach构造不营业数组本身,而是它的一个副本。在每次循环中,变量$值的值可

以操作,但该数组的原始值保持不变。

标签:[!--infotagslink--]

您可能感兴趣的文章: