简体   繁体   中英

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

<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

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. 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. You can configure that on your server or you can register on a cronjob site. There are plenty of sites where you can register cron jobs for free p.ex. http://www.onlinecronjobs.com/ But I suggest to register it on your own server.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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