如何使用crontab来编辑PHP文件中的变量? [英] How to use a crontab to edit variables within a PHP file?

查看:229
本文介绍了如何使用crontab来编辑PHP文件中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Cronjob从PHP文件编辑变量?



基本上我有一个PHP文件,如下所示:

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

我想要的是每24小时通过将日数增加一来改变它,txid



因此,在cron作业运行24小时之后,上面的代码之后,它将如下所示:

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

可以吗?



非常感谢,非常感谢您收到的任何帮助。

解决方案

忘记此任务的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.

这篇关于如何使用crontab来编辑PHP文件中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆