For loop time update(H:i) with 10 minutes in php - php

I am trying to update time in for loop in php and i am getting unexpected results.
I have tried using while loop still same results. Dont know where its going wrong.
$time = strtotime(date('H:i'));
$trounds= 15;
for($j = 1; $j<= $trounds ; $j++){
echo $time += date("H:i:s", strtotime("+10 minutes", $time));
echo date('H:i:s',$time);
echo '</br>';
}
Suppose if the time is 12:00 i need output like
12:10
12:20
12:30
and so on.

I think you don't need date() and strtotime() twice , Try like below
$time = strtotime(date('4:i'));
$trounds= 15;
for($j = 1; $j<= $trounds ; $j++){
echo $time += 10*60;
echo ' : '.date('H:i:s',$time);
echo "\n";
}
Live Example
Output :
1563017220 : 04:27:00
1563017820 : 04:37:00
1563018420 : 04:47:00
1563019020 : 04:57:00
1563019620 : 05:07:00
1563020220 : 05:17:00
1563020820 : 05:27:00
1563021420 : 05:37:00
1563022020 : 05:47:00
1563022620 : 05:57:00
1563023220 : 06:07:00
1563023820 : 06:17:00
1563024420 : 06:27:00
1563025020 : 06:37:00
1563025620 : 06:47:00

<?php
$date = new DateTime();
echo "Current Date Time: "; print_r($date); echo "</br>";
$trounds= 15;
for($j = 0; $j< $trounds ; $j++){
$date->modify("+10 minutes");
print_r($date->format('H:i')); echo '</br>';
}
exit;
Use PHP DateTime Class For Better Usage and Flexibility. You can read and learn more about from PHP official docs : PHP DateTime Class. You can make use of various other methods that are provided by the DateTime Class.

You are adding the incorrect values in your $time after the just after the for loop which is making it wrong
$time = strtotime(date('H:i'));
$trounds= 15;
for($j = 1; $j<= $trounds ; $j++){
echo $time = strtotime("+10 minutes", $time); //echo $time += date("H:i:s", strtotime("+10 minutes", $time));
echo date('H:i:s',$time);
echo '</br>';
}
You're adding the string format ("H:i:s") value into the float value.. Check the above code you just need to assign the strtotime value and it will do rest.

Related

Adding month in php?

month adding problem
<?php
$showMonthsQty = 3;
for($i = (1-$showMonthsQty); $i <= 0; $i++)
{
echo $date = date("Y-m-1", strtotime(" +$i months"));
}
?>
when it run's im not geting the desired answer.
OUTPUT
2018-03-1
2018-05-1 <----------error
2018-05-1
but i needed output is :
2018-03-1
2018-04-1
2018-05-1
how can i get this ?pls help!... tnx in advance...:)
You can use DateTime and DateInterval.
$showMonthsQty = 3;
for($i = $showMonthsQty-1; $i >= 0; $i--)
{
$date = new \DateTime(date("Y-m-1")); // First day of the current month
$date->sub(new \DateInterval(sprintf('P%sM', $i))); // Substract $i month (P%dM)
echo $date->format('Y-m-d')."<br />";
}
Output:
2018-03-01
2018-04-01
2018-05-01
Is it what you're looking for ?

Working dates between two given dates in Php

Please, i need assistance in this code.I have checked others in Stakeoverflow, but it is not combatible, hence this question. I want to generate all working /weekdays between two dates.I have found a code, but it is generating all days, including weekend. How do i eliminate the weekend from the list or ensure the list generated is ONLY for weekdays?
<?php
$start_Date = date('Y-m-d');
$end_Date = date('Y-m-d', strtotime('30 weekdays'));
//echo $start_Date."<br/>";
//echo $end_Date."<br/>";
// Specify the start date. This date can be any English textual format
$date_from = $start_Date;
$date_from = strtotime($date_from); // Convert date to a UNIX timestamp
// Specify the end date. This date can be any English textual format
$date_to = $end_Date;
$date_to = strtotime($date_to); // Convert date to a UNIX timestamp
// Loop from the start date to end date and output all dates inbetween
$c = 0;
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
I expect 30days to be generated but with this code, I am getting 42days . Weekend has been added,instead of weekdays ONLY .
Just add this to your loop:
$w = date('w',$i);// day of week - Sunday == 0, Saturday == 6
if($w == 0 || $w == 6){
continue;
}
DEMO
Your code is almost working only have to add a if checking in your code
your code
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
please replace with that one
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$day = date("w", $i);
if($day != 0 && $day!= 6){ // will continue if not Sunday or Saturday
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
}
You also can take help from php.net
Thanks
You may need to get the day of the week, like date("D"), then use it in your for loop to check..something like this?:
$Weekends = array("Sat","Sun");
for....
$DayOfWeek = date("D",$i);
if(!in_array($DayOfWeek, $Weekend)){
// increment...
}

how to get 4-days ago to now - php

If current date is 2016-03-06, I would like to get these dates :
2016-03-06
2016-03-05
2016-03-04
2016-03-03
I'm trying to get this purpose but my result not what I want :
$_4date = date("y-m-d",strtotime("day"));
$_3date = date("y-m-d",strtotime("-1 day"));
$_2date = date("y-m-d",strtotime("-2 day"));
$_1date = date("y-m-d",strtotime("-3 day"));
echo $_4date;
echo '<br />';
echo $_3date;
echo '<br />';
echo $_2date;
echo '<br />';
echo $_1date;
the result is :
70-01-01
16-03-05
16-03-04
16-03-03
To get today's date with strtotime, you do strtotime("today");. However, as Bjorn has commented, you can simply just call date() directly.
Furthermore, the reason you are not getting the year in four digits is because you are using a lowercase y instead of an uppercase Y.
Try date("Y-m-d", strtotime("-1 day"));.
The following piece of code illustrates the required changes:
$today = date("Y-m-d");
$yesterday = date("Y-m-d", strtotime("-1 day"));
echo "$today <br />";
echo "$yesterday <br />";
// Output
2016-03-06
2016-03-05
For more informtation, please consult the PHP documentation on the date function. It actually shows you that what to expect from y and Y and it also shows you that the default value that is passed as the second argument is time(), meaning the default is the current time.
PHP's strtotime documentation can be consulted for more information on the strtotime() function and its possible parameters.
Always check the (PHP) documentation first before asking a question.
You need to use like that:
$_4date = date("Y-m-d");
$_3date = date("Y-m-d",strtotime("-1 day"));
$_2date = date("Y-m-d",strtotime("-2 day"));
$_1date = date("Y-m-d",strtotime("-3 day"));
Explanation:
For current date no need to use use strtotime().
For full year you need to use this format Y-m-d.
y-m-d will return you the date 16-03-06 but Y-m-d will return you 2016-03-06.
Use a for loop with strtotime( "... days ago" ):
for( $i = 0; $i < 4; $i++ )
{
echo date( 'Y-m-d', strtotime( "$i days ago" ) ) . PHP_EOL;
}
The first loop (0 days ago) will output today date, other loops will output past days.
3v4l.org demo
You need to use capital 'Y' for full year (2016) instead of small 'y' which will display year in shorthand (16).
And for current date just use date("Y-m-d").
<?php
$_4date = date("Y-m-d");
$_3date = date("Y-m-d",strtotime("-1 day"));
$_2date = date("Y-m-d",strtotime("-2 day"));
$_1date = date("Y-m-d",strtotime("-3 day"));
echo $_4date;
echo '<br />';
echo $_3date;
echo '<br />';
echo $_2date;
echo '<br />';
echo $_1date;
?>
Working Example
<?php
$date = [date("Y-m-d")];
for($i = 1; $i < 4; $i++) {
$date[] = date("Y-m-d",strtotime("-$i day"));
}
//For cli output you'll need:
echo implode("\n", $date) . "\n";
//For web output you'll need:
echo implode("<br />", $date) . "<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.

Compare two time values using jQuery's validate

I have two selects, one that is a time from and the other is a time to. These values are split by fifteen minute integrals. I take those values and convert them using PHP's strtotime. I would like to compare these values to make sure that the time to is not lower than the time from or that the time from is not higher than the time to using jQuery's validate.
Thanks.
Here is my PHP:
$start = strtotime('12:00 AM');
$end = strtotime('12:00 PM');
echo '<select name="TimeFrom" id="TimeFrom" class="timeSelectFrom">';
for ($i = $start; $i <= $end; $i += 900)
{
echo '<option>' . date('g:i A', $i) . '</option>';
}
echo '<option>Closed</option>';
$start = strtotime('12:00 AM');
$end = strtotime('12:00 PM');
echo '<select name="TimeTo" id="TimeTo" class="timeSelectTo">';
for ($i = $start; $i <= $end; $i += 900)
{
echo '<option>' . date('g:i A', $i) . '</option>';
}
echo '<option>Closed</option>';
echo '</select>';
You could easily translate the values from the form elements into a basic time-stamp using a simplistic formula like (seconds+60*minutes) and then compare them as required.

Categories