简体   繁体   English

在PHP中每48小时计算一次

[英]calculating every 48 hours in php

Here, I am not getting the trick to update the database exactly after 48 hours only once in php. 在这里,我没有办法在48小时内仅在php中更新一次数据库。 I have use this trick using modulo but this doesn't gives exact output. 我已经使用了使用模数的技巧,但这不能给出确切的输出。 In below code, the page is refresh that is executed every 45 sec of last 48 th hours. 在以下代码中,页面是刷新的,最近48小时每45秒执行一次。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<META HTTP-EQUIV="REFRESH" CONTENT="45">
</head>
<body>
<?php
$date1 = date('Y-m-d h:i:s'); 
$date2 = "2012-02-29 12:59:00"; 
$diff = abs(strtotime($date2) - strtotime($date1)); 
$hours = $diff/(60*60);
echo $hours . "<h1> NeVeR CloSe ThIs PaGe ......</h1>";
if($hours%48 == 0)
{
$sql = "UPDATE [db].[dbo].[table] set status = 0";
$res = odbc_exec($con,$sql) or die(odbc_error());
 } 
?>
</body>
</html>

How can I execute update query only once in every 48 hours starting from date: 2012-02-29 12:59:00 从日期开始,如何每48小时仅执行一次更新查询:2012-02-29 12:59:00

To avoid that the UPDATE is executed more than once in 48 hours i would suggest you compare the current time with the time of the last execute before you send the UPDATE query. 为避免UPDATE在48小时内多次执行,我建议您在发送UPDATE查询之前将当前时间与上次执行时间进行比较。 But to compare that you have to save the datetime stamp of the last execution somewhere. 但是要进行比较,您必须将上一次执行的日期时间戳保存在某个地方。 I would save it into a new table of your database. 我会将其保存到数据库的新表中。

For the update every 48 hours i would suggest to use a cron job. 对于每48小时进行一次更新,我建议您使用Cron作业。 You can configure that on your server or you can register on a cronjob site. 您可以在服务器上进行配置,也可以在cronjob网站上进行注册。 There are plenty of sites where you can register cron jobs for free p.ex. 您可以在许多网站上免费注册cron职位。 http://www.onlinecronjobs.com/ But I suggest to register it on your own server. http://www.onlinecronjobs.com/但我建议在您自己的服务器上注册。

Since SQL Server express does not come with SQL Agent, you can use the Windows scheduler to run a SQLCMD with a stored proc or a SQL script. 由于SQL Server Express不随SQL Agent一起提供,因此您可以使用Windows调度程序通过存储的proc或SQL脚本运行SQLCMD。 MSDN Link MSDN链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM