Inserting an image multiple times based on difference between 2 numbers. PHP - php

working on a script, that calculates the difference between dates retrieved from an API. I want it to then drop in an image as many times as the difference.
$d1 = new DateTime('2012-04-04');
$d2 = new DateTime('2012-03-31');
$interval = $d1->diff($d2);
Ive tried to have a go with a for loop but with no success
for ($i = 0 $i <= $interval; $i++)
{
echo = "<img src=\"test.jpg"/";
}
can you guys see any problems?

I find PHP's DateTime works better when you use the modify() method instead of normal arithmetic.
Assuming you want one image per day:
$d1 = new DateTime('2012-04-04');
$d2 = new DateTime('2012-03-31');
while($d2 <= $d1)
{
echo "<img src=\"test.jpg\">";
$d2->modify("+1 day");
}

This should do it:
<?php
$d1 = strtotime('2012-04-04');
$d2 = strtotime('2012-03-31');
$interval=($d1-$d2)/(3600*24);
for ($i = 0; $i <= $interval; $i++)
{
echo '<img src="test.jpg">';
}
?>

Related

How to loop 2 dates to output like this?

Here's my code that display date with for loop.
Topic: Im creating a script to generate payment due(from to start).
$y = 1;
$period = 3;
$start = date('m/15/Y');
echo "<table>";
echo '<thead><th>From</th>';
echo '<th>To</th></thead>';
for ($y; $y <= $period; $y++) {
$month_mid = date("m/15/Y", strtotime($start));
$month_last = date("m/t/Y", strtotime($start));
echo '<td>'.$month_mid = date("m/t/Y", strtotime($start)).'</td>';
echo '<td>'.$month_last = date("m/15/Y", strtotime($start)).'</td></tr>';
$start = date("m/d/Y",strtotime($start." +1month"));
}
echo '</table>';
output I get:
09/15/2017 09/30/2017
10/15/2017 10/31/2017
11/15/2017 11/31/2017
I want to appear like this:
09/15/2017 09/30/2017
09/30/2017 10/15/2017
10/15/2017 10/31/2017
Im new in date php hope you can help me with this thanks.
Here you go:
$y = 1;
$period = 5;
$start = date('m/15/Y');
echo "<table>";
echo '<thead><th>From</th>';
echo '<th>To</th></thead>';
for ($y; $y <= $period; $y++) {
$month_mid = date("m/15/Y", strtotime($start));
$month_last = date("m/t/Y", strtotime($start));
echo '<tr><td>'.$month_mid = date("m/t/Y", strtotime($start)).'</td>';
echo '<td>'.$month_last = date("m/15/Y", strtotime($start)).'</td></tr>';
$start = date("m/d/Y",strtotime($start." +1month"));
}
echo "</table>";
You missed the opening and clousure of table and <tr>.
You don't want to be skipping ahead by a month in your loop. Well, not the way you are doing it here.
You should use DatePeriod::getEndDate and DatePeriod::getStartDate, along with DateTime::add to skip by semi-monthly amounts. The idea is that you only get to the next month by letting the datetime API add 15 days to a given start date, and use that to figure out the mid-month and end-month dates.
Keep everything in these date objects until you need to format and print them, and save the second one in the pair as input for the next round of the loop at the end where you just have to calculate the new second value.
I feel like you could write a function that gets the "next" date from any other first or last date of the month to simplify the loop.
(Since this is semi-monthly and not bi-weekly, you can actually just walk the months in your period, hard-coding the 15th for one value and using your end-of-month function for the second. It depends on your requirements and how complicated the API gets.)
Or
For each month in your period, calculated the mid-month (i.e., exactly 15 days from the start of the month) and end-month dates. Save them in a Collection of couplets of some sort.
Write a display routine that takes this Collection and outputs the dates, but saves the previous formatted string made from the second item in the couplet as the first item to be printed (after the first line.)
I actually prefer this one because it separates the presentation from the data abstraction, allowing you freedom to display and format the date how you see fit.
But, at the end of the day (pun not intended, but what a great pun), stop using date strings as input to figure out other dates when you have access to normalized epoch representations. This will only lead to madness.
$y = 1;
$period = 3;
$start = date('m/d/Y');
$end = date('m/t/Y');
echo "<table>";
echo '<thead><th>From</th>';
echo '<th>To</th></thead>';
for ($y; $y <= $period; $y++) {
echo '<tr><td>'.$start.'</td>';
echo '<td>'.$end.'</td></tr>';
$getLast = date('d',strtotime($end));
if($getLast >= 28) {
$start = date("m/t/Y", strtotime($start));
$end = date("m/d/Y", strtotime("+15 day", strtotime($end)));
}else {
$start = date("m/d/Y", strtotime("+15 day", strtotime($start)));
$end = date("m/t/Y",strtotime($end));
}
}
echo "</table>";
Result:
From To
09/15/2017 09/30/2017
09/30/2017 10/15/2017
10/15/2017 10/31/2017

change image in every three days using php

I am creating a wordpress widget. This will allow user to upload the image url of eight different picture. Now I want to change the image everty three days and start from begining after eight one for infinite time.
I manage to get the start of the first image. let sat that is : 2014/07/28
I have applied this logic:
$date = $start_date;
$date = strtotime($date);
$date = strtotime("+3 day", $date);
$end_date = date('Y-m-d', $date);
$begin = new DateTime( $start_date );
$end = new DateTime('');
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
if($start_date <= $end_date && $end_date > date('Y-m-d'))
{
$image_number = 1;
}else{
$i = 1;
$j = 1;
foreach($daterange as $date){
if($i%3 == 0){
echo $image_number = $j;
$j++;
if($j > 8){
$j = 1;
}
}
$i++;
}
}
?>
" alt="">
Can anybody tell me what is wrong with the code.
Thank you everybody in advance for your valuable time.
Alright so it seems you have 8 images named 1.jpg to 8.jpg
Every three days you want to change the image to the next, and when you are at 8, you want to go to 1.
I don't know if you have some kind of start date on which the images were uploaded, and I don't know if it matters to you.
Let's start out with an example in which viewing the image is tied to the date (and resets on the first of january)
##maybe you would like to set the timezone
##date_default_timezone_set('America/Los_Angeles');
$dayOfTheYear = date('z');
$iterationCount = ceil($dayOfTheYear / 3);
$numberForIteration = $iterationCount % 8;
echo '<img alt="" src="'.$numberForIteration.'.jpg" />';
## prints for example: <img alt="" src="6.jpg" />
We can build on this and have an option of a different start date, meaning that not january 1st, but another date is the start date.
## PHP Version should be above June 2012
$startDate = new DateTime("2014-07-01");
$currentDate = new DateTime();
$daysSinceStart = $startDate->diff($currentDate)->days;
$iterationCount = ceil($daysSinceStart / 3);
$numberForIteration = $iterationCount % 8;
echo '<img alt="" src="'.$numberForIteration.'.jpg" />';
Hope it helps.
David

PHP strtotime error when convert some date string to datetime value

I have a trouble with strtotime in php.
$j = "2013-10-27";
for ($x = 0; $x < 100; $x++) {
$j = date('Y-m-d', strtotime($j) + 86400);
echo ' '.$j.' <br/>';
}
As the code is self explained, it will add one day to $j and then display to browser.
But when $j = "2013-10-27", it prints just one result ("2013-10-27"). If I change $j to another date, this does work, but also stucks at this date (and some other date).
I have written other code to do this work.
But does anyone know why it fails, or my code is wrong?
Thank you.
This is because you are in a timezone with daylight savings time, and on the 27th October at 1 AM the time reverted to midnight, making it a 25 hour day.
This can be reproduced by setting the timezone:
<?php
date_default_timezone_set('Europe/London');
$j = "2013-10-27";
for ($x = 0; $x < 100; $x++) {
$j = date('Y-m-d', strtotime($j) + 86400);
echo ' '.$j.' <br/>';
}
http://codepad.viper-7.com/uTbNWf
strtotime has too many limitations in my opinion. Use the more recent DateTime lib instead
$j = new DateTime('2013-10-27');
$interval = new DateInterval('P1D');
for ($x = 0; $x < 100; $x++) {
$j->add($interval);
echo $j->format('Y-m-d'), '<br>';
}

Reverse whole date in php

OK,
i was using following code to reverse a date to use in php yesterday.
<?php
$d=date("F j Y");
$d=explode(" ", $d);
$month=$d[0];
$day=$d[1];
$year=$d[2];
<?php for ($i=10; $i>0; $i--)
{
$date=$month."-".$day--."-".$year;
echo $date;
?>
this was working for me till yesterday 31 March Night. but in the morning on april 1 its started printing
April-1-2012
April-0-2012
April--1-2012 April--2-2012
and so on.
this was the bad logic i used to reverse a date. i realized it soon.
i want this like following.
April-1-2012
March-31-2012
March-30-2012
March-29-2012
and so on
so how this could be possible ?
Thanks in advance.
Well, this also a logic that work perfect for me i made after post of question. but i am really thankful for all answers. that also making me many things clear.
<?php
$d=date("F j Y");
for ($i=0; $i>-10; $i--)
{
$date="<br>".date('F-j-Y', strtotime("+$i days"));
echo $date;
}
?>
This is probably the quickest way to do it
for($i=1; $i <= 10; $i++) {
echo date("F j Y", time()-($i*24*60*60)); //Instead of 24*60*60 write 86400 to increase slight performance
}
Demo
To get an incremental decrease:
$date = date("F j Y");
for ($i = 0; $i < 10; ++$i)
{
$dates[] = date("F-j-Y", strtotime($date . ' - ' . $i . ' day'));
}
If you are using PHP5.2, you can use the DateTime Class and the modify method.
http://www.php.net/manual/en/datetime.modify.php
<?php
$date = new DateTime('2006-12-12');
$date->modify('-1 day');
echo $date->format('F-j-Y');
?>
In 5.3, you can use the sub method in the DateTime class: http://www.php.net/manual/en/datetime.sub.php
<?php
$date = new DateTime('2000-01-20');
$date->sub(new DateInterval('P1D'));
echo $date->format('F-j-Y');
?>
So an example, using the PHP 5.2 DateTime class (example here: http://ideone.com/uwkjj):
$date = new DateTime();
echo $date->format('F-j-Y');
for($i = 0; $i < 10; $i++){
$date->modify('-1 day');
echo $date->format('F-j-Y');
}
And here is an example using PHP5.3 DateTime class with the sub method
$date = new DateTime();
echo $date->format('F-j-Y');
for($i = 0; $i < 9; $i++){
$date->sub(new DateInterval('P1D'));
echo $date->format('F-j-Y');
}
There is also an interesting note on the PHP help page for strtotime: http://www.php.net/manual/en/function.strtotime.php that says not to use it for mathematical operations. I wonder what that means. But I guess subtracting 1 day should be fine.
From PHP manual:
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.

Fill Out The Gaps Between Two Times With PHP

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)));
}

Categories