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.
Related
Gooooood evening all...
Edit:
I'm trying to write a php script to output the dates I will work for the remainder of the year if given a specific start date.
For example I start on 2020-05-14 and work for 4 days then take 4 days off.
This continues for the remainder of the year.
I would like to output the dates i will work and think it could probably be done using the php for loop, however i've been thinking of how to do this for too long and can't seem to break through the wall.
The start of my code is this:
<?php
$z = date("z",mktime(0,0,0,05,14,2020));
for($i=$z;$i<=365;$i+=4) {
echo("$i<br>");
}
?>
Any advice would be much appreciated on a possible solution.
You're computing the day of the year (e.g. for 2020-05-14, that would be 134), which is fine for letting your script know when to stop.
However, you're wanting to output dates, where you're currently outputting the day of the year instead.
I think there's an easier way to do what you're trying to accomplish.
<?php
$start = new \DateTime("2020-05-14 00:00:00");
$end = new \DateTime($start->format("Y") . "-12-31 00:00:00");
$four = new \DateInterval("P4D");
$one = new \DateInterval("P1D");
$format = 'Y-m-d';
$date = $start;
while ($date <= $end) {
echo $date->format($format), PHP_EOL;
for ($day = 2; $day <= 4; $day++) {
$date->add($one);
echo $date->format($format), PHP_EOL;
}
$date->add($four);
}
I am trying to group an index by weeks as opposed to days like the code currently does. My code for displaying the days since the start date (FIRST_DAY_STRING) and time on that day (TIME_SUFFIX) is below. I am having just a bit of trouble converting it to weeks since the start day instead of days.
Thanks for any help in advance. I would also appreciate an explanation of how to do it for months as well as I also need that.
<?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
?>
I did it myself. What I did was divide the inval($days) by 7 and rounded it to the nearest whole number:
$day_number = round(intval($days) / 7 + 1, 0);
Hello I try to take the difference between two dates and display it.
My problem is that the time difference I get is not the correct one.
This is my code:
$time1 = strtotime('2014-03-28 15:20:00');
$time2 = strtotime('2014-03-28 15:15:00');
$diffTime = $time1 - $time2;
echo date('H:i', $diffTime);
The result I get is:
02:05
The currect time should be this:
00:05
My guess that the date somehow takes timezone or something like this but Im not sure.
Thanks.
/****************************************
$start_date = new DateTime('23:58:40'); *These two still give
$end_date = new DateTime('00:00:00'); *a wrong answer
*****************************************/
$start_date = new DateTime('23:58:40');
$end_date = new DateTime('00:11:36');
$dd = date_diff($end_date, $start_date);
//Giving a wrong answer: Hours = 23, Minutes = 47, Seconds = 4
echo "Hours = $dd->h, Minutes = $dd->i, Seconds = $dd->s";
So what you're actually doing here is generating two UNIX timestamps (numbers) and then subtracting them. then you're passing the resulting number as if it were still a timestamp to date().
essentially $diffTime is the number of seconds between your two times. you could divide by 60 to get minutes, and so on and so forth, but PHPs DateTime objects are much better.
From the PHP docs:
http://pl1.php.net/strtotime
Note:
Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.
try this
<?php
$time1 = strtotime('2014-03-28 15:20:00');
$time2 = strtotime('2014-03-28 15:15:00');
echo round(abs($time1 - $time2) / 60,2). " minute"
?>
Below is the solution of date time in years,days.hours,minutes and seconds.
$time1 = strtotime('2014-03-28 15:20:00');
$time2 = strtotime('2014-03-28 15:15:00');
$diffTime = $time1 - $time2;
$y = ($diffTime/(60*60*24*365));
$d = ($diffTime/(60*60*24))%365;
$h = ($diffTime/(60*60))%24;
$m = ($diffTime/60)%60;
$s = ($diffTime)%60;
echo "Minutes - " .$m;
echo "<br/>";
I am having table time in mysql database with one attribute of type "TIME" which contains default value "09:00:00". what I am trying to do is to get this value and subtract it from current time.
include 'connection.php';
$time = mysql_query("SELECT start_time FROM time");
$s = mysql_fetch_assoc($time);
$start_time = strtotime($s['start_time']);
$time_now = date("H:i:s");
$delay = ($time_now - $start_time);
However it never worked the way I need. result always like 00:00:00
what i want to achieve is something like:
$start_time = 09:00:00
$time_now = 09:34:23
so $delay should be 00:34:23.
any help to achieve that?
Thanks in advance.
Just a small bug in your code.... look below
include 'connection.php';
$time = mysql_query("SELECT start_time FROM time");
$s = mysql_fetch_assoc($time);
$start_time = strtotime($s['start_time']);
$time_now = date("H:i:s");
$delay = ($time_now - $start_time); //BUG! String minus timestamp here...
Fix that second block with:
$start_time = strtotime($s['start_time']);
$delay = date( "H:i:s", time() - $start_time );
Agree with others though, this is really cleanly done on the database side as well.
Hope this helps
I've got to write a loop that should start and end between two times. I know there are many ways to skin this cat, but I'd like to see a real programmers approach to this function.
Essentially I have Wednesday, for instance, that opens at 6:00pm and closes at 10:30pm.
I'm looking to write a loop that will give me a table with all of the times in between those two in 15 minute intervals.
So, I basically want to build a one column table where each row is
6:00pm
6:15pm
7:15pm
etc...
My two variables to feed this function will be the open time and the close time.
Now don't accuse me of "write my code for me" posting. I'll happily give you my hacked solution on request, I'd just like to see how someone with real experience would create this function.
Thanks :)
$start = new DateTime("2011-08-18 18:00:00");
$end = new DateTime("2011-08-18 22:30:00");
$current = clone $start;
while ($current <= $end) {
echo $current->format("g:ia"), "\n";
$current->modify("+15 minutes");
}
Try it on Codepad: http://codepad.org/JwBDOQQE
PHP 5.3 introduced a class precisely for this purpose, DatePeriod.
$start = new DateTime("6:00pm");
$end = new DateTime("10:30pm");
$interval = new DateInterval('PT15M');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $time) {
echo $time->format('g:ia'), PHP_EOL;
}
echo $end->format('g:ia'); // end time is not part of the period
$start = strtotime('2011-08-11 18:00:00');
for ($i = 0; $i < 20; $i++) {
echo date('g:ia', $start + ($i * (15 * 60))), '<br>';
}
I would go with the DateTime functions and increase the time by 15 minutes every loop-turn as long as the current time is lower then the end-time.
EDIT: as user576875 has posted
$start_date = '2019-07-30 08:00:00';
$end_date = '2019-09-31 08:00:00';
while (strtotime($start_date) <= strtotime($end_date)) {
echo "$start_date<br>";
$start_date = date ("Y-m-d H:i:s", strtotime("+1 hours", strtotime($start_date)));
}