且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用crontab来编辑PHP文件中的变量?

更新时间:2023-09-08 13:14:52

忘记此任务的cron

即使没有数据库 - 如果由于某种原因您不想使用它:

 <?php 
define(FIRST_DAY_STRING,2014-3-26);
define(SHIFT_DAYS,'P2D');
define(TIME_SUFFIX,20:30:00 GMT + 11:00);

$ today = new DateTime();
$ first_day = new DateTime(FIRST_DAY_STRING);
$ interval = $ first_day-> diff($ today);
$ days = $ interval->格式('%R%a days');
$ end_date = $ today-> add(new DateInterval(SHIFT_DAYS));

$ day_number = intval($ days)+ 1;
$ txid =tx $ day_number;
$ end_time = $ end_date-> format('Y-n-j')
$ end_time。= TIME_SUFFIX

此示例假设您从2014-3-26(第1天)开始计算天数,总是2天后在20:30:00。您可以更改常量以获得不同的行为。


Is it possible to edit variables from a PHP file with the use of Cronjob? If so, how would I go about doing this?

Basically I have a PHP file that looks like this:

<?php
$daynumber = "1";
$txid = tx1;
$endtime = "2014-3-28 20:30:00 GMT+11:00";
?>

What I want is that every 24 hours it changes it by increasing the day number by one and the txid by one.

So basicaly after the cron job running 24 hours after the code above it will look like this:

<?php
$daynumber = "2";
$txid = tx2;
$endtime = "2014-3-29 20:30:00 GMT+11:00";
?>

Is it possible to do? If not, what other way could I produce the same result.

Thank you very much, I appreciate any help I receive.

Forget about cron for this task
Even without the database - if for some reason you don't want to use it:

<?php
define("FIRST_DAY_STRING", "2014-3-26");
define("SHIFT_DAYS", 'P2D');
define("TIME_SUFFIX", " 20:30:00 GMT+11:00");

$today = new DateTime();
$first_day = new DateTime(FIRST_DAY_STRING);
$interval = $first_day->diff($today);
$days = $interval->format('%R%a days');
$end_date = $today->add(new DateInterval(SHIFT_DAYS));

$day_number = intval($days) + 1;
$txid = "tx$day_number";
$end_time = $end_date->format('Y-n-j')
$end_time .= TIME_SUFFIX
?>

This example assumes that you start counting days from 2014-3-26 (day 1) and the endtime is always 2 days later at 20:30:00. You can alter the constants to get a different behavior.