How to add 12 hours to the current date, save it in an accumulator and re-add in php

1

I am learning php and I have this code:

<?php
date_default_timezone_set('America/Monterrey');
$f = date (" d\of F\of Y, H:i");
echo $f, "<BR>";
setlocale(LC_TIME, "es_Mx.utf8");
$f2=strftime("Today is %A %d of %B of %G and is %H:%M %p");
echo $f2."<BR>";
$time=strtotime("31 january 2017");
$f3 = date ("j/n/Y", $time);
echo $f3."<BR>";
$f4=strftime("%A %d of %B of %G and is %H:%M %p", time() + 12*60*60);
for($i=0; $i<10; $i++){
echo $f4."<BR>";
}
?>

and print this:

When should I print this:

Thursday 16 of February of 2017 and is 07:21 AM

Thursday 16 of February of 2017 and is 07:21 PM

Friday 17 of February of 2017 and is 07:21 AM

and so on, what can I add to my code so that it is complete?

    
asked by Wolvy 16.02.2017 в 02:27
source

1 answer

1

When you declare

$f4=strftime("%A %d of %B of %G and is %H:%M %p", time() + 12*60*60);
for($i=0; $i<10; $i++){
  echo $f4."<BR>";
}

your variable $f4 is defined only once, and remains with that date forever. Then in the loop for you print it 10 times.

What you must do is store the date before entering the loop and modify it within it, in each iteration:

$tiempo = time();
for($i=0; $i<10; $i++){
   $tiempo = $tiempo + 12*60*60*$i; 
   $f4=strftime("%A %d of %B of %G and is %H:%M %p", $tiempo);
   echo $f4."<BR>";
}
    
answered by 16.02.2017 / 06:36
source