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.
Related
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.
I need help Select every other Wednesday starting on 5/2/12. This code below selects every other Wednesday starting on the week it currently is. But i need to set the beginning week. I am familiar with PHP, but not familiar with php dates. So please be as specific as possible.
I found this:
$number_of_dates = 10;
for ($i = 0; $i < $number_of_dates; $i++) {
echo date('m-d-Y', strtotime('Wednesday +' . ($i * 2) . ' weeks')). "<br>".PHP_EOL;
}
Use mktime to create your starting date and pass that as the second argument to strtotime so that counting starts from there:
$startDate = mktime(0, 0, 0, 5, 2, 2012); // May 2, 2012
for ($i = 0; $i < $number_of_dates; $i++) {
$date = strtotime('Wednesday +' . ($i * 2) . ' weeks', $startDate);
echo date('m-d-Y', $date). "<br>".PHP_EOL;
}
See it in action.
Give it a date in the string, instead of "Wednesday" (that chooses the next Wednesday), write:
strtotime('20120502 +' . ($i * 2) . ' weeks'))
To choose that date. (Format is yyyymmdd).
If you have PHP 5.2.0 or newer, you can do it easily this way:
$date = new DateTime('2006-05-02');
for ($i=0; $i<10; $i++) {
echo $date->format('m-d-Y').'<br/>'.PHP_EOL;
$date->modify('+1 week');
}
You could also use the DatePeriod and DateInterval classes to make life easier.
Standard disclaimer: both of the classes above require PHP >= 5.3.0.
$number_of_dates = 10;
$start_date = new DateTime("5/2/12");
$interval = DateInterval::createFromDateString("second wednesday");
$period = new DatePeriod($start_date, $interval, $number_of_dates - 1);
foreach ($period as $date) {
echo $date->format("m-d-Y") . "<br>" . PHP_EOL;
}
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">';
}
?>
I have 2 dates. Lets say they look like this.
$start = 2010/12/24;
$end = 2012/01/05;
I query the database to look for visits between these two dates. I find some. I then populate an array called stats.
$stats['2010/12/25'] = 50;
$stats['2010/12/31'] = 25;
...
As you can see, there are days missing. I need to fill the missing dates with a value of zero. I was thinking something like this. (I have pulled day / month / year from start and end dates.
for($y=$start_year; $y <= $end_year; $y++) {
for($m=$start_month; $m <=$end_month; $m++) {
for($d=$start_day; $d <= $end_day; $d++) {
This would work fine for the year however the months and days wouldn't work. If the start day is the 15th. Days 1-14 of each subsequent month would be missed. I could have a solution like this then...
for($y=$start_year; $y <= $end_year; $y++) {
for($m=1; $m <13; $m++) {
$total_days = cal_days_in_month(CAL_GREGORIAN, $m, $y) + 1;
for($d=1; $d <= $total_days; $d++) {
I would then need a bunch of if statements making sure starting and end months and days are valid.
Is there a better way of doing this? Or could this even be done in my mysql query?
Just to demonstrate the power of some of PHP's newer interval handling method (mentioned by pgl in his answer):
$startDate = DateTime::createFromFormat("Y/m/d","2010/12/24",new DateTimeZone("Europe/London"));
$endDate = DateTime::createFromFormat("Y/m/d","2012/01/05",new DateTimeZone("Europe/London"));
$periodInterval = new DateInterval( "P1D" ); // 1-day, though can be more sophisticated rule
$period = new DatePeriod( $startDate, $periodInterval, $endDate );
foreach($period as $date){
echo $date->format("Y-m-d") , PHP_EOL;
}
Does require PHP >= 5.3.0
EDIT
If you need to include the actual end date, then you need to add a day to $endDate immediately before the foreach() loop:
$endDate->add( $periodInterval );
EDIT #2
$startDate = new DateTime("2010/12/24",new DateTimeZone("Europe/London"));
$endDate = new DateTime("2012/01/05",new DateTimeZone("Europe/London"));
do {
echo $startDate->format("Y-m-d") , PHP_EOL;
$startDate->modify("+1 day");
} while ($startDate <= $endDate);
For PHP 5.2.0 (or earlier if dateTime objects are enabled)
If you're using PHP5.3 then Mark Baker's answer is the one to use. If (as you say in your comment) you're still on PHP5.2 something like this should help you:
$startdate = strtotime( '2010/12/24' );
$enddate = strtotime( '2012/01/05' );
$loopdate = $startdate;
$datesArray = array();
while( $loopdate <= $enddate ) {
$datesArray[$loopdate] = 0;
$loopdate = strtotime( '+1 day', $loopdate );
}
It will create an array of the unix timestamp of every date between the start and end dates as the index and each value set to zero. You can then overwrite any actual results you have with the correct values.
$start_date = DateTime::createFromFormat('Y/m/d', '2010/12/24');
$end_date = DateTime::createFromFormat('Y/m/d', '2012/01/05');
$current_date = $start_date;
while($current_date <= $end_date) {
$current_date = $current_date->add(new DateInterval('P1D'));
// do your array work here.
}
See DateTime::add() for more information about this.
$i = 1;
while(date("Y/m/d", strtotime(date("Y/m/d", strtotime($start)) . "+ $i days")) < $end) {
... code here ...
$i++;
}
I would calculate the difference between start and end date in days, iterate on that adding a day to the timestamp on each iteration.
$start = strtotime("2010/12/24");
$end = strtotime("2012/01/05");
// start and end are seconds, so I convert it to days
$diff = ($end - $start) / 86400;
for ($i = 1; $i < $diff; $i++) {
// just multiply 86400 and add it to $start
// using strtotime('+1 day' ...) looks nice but is expensive.
// you could also have a cumulative value, but this was quicker
// to type
$date = $start + ($i * 86400);
echo date('r', $date);
}
I have this bit of horrible code saved:
while (($tmptime = strtotime('+' . (int) $d++ . ' days', strtotime($from))) && ($tmptime <= strtotime($to))) // this code makes baby jesus cry
$dates[strftime('%Y-%m-%d', $tmptime)] = 0;
(Set $from and $to to appropriate values.) It may well make you cry, too - but it sort of works.
The proper way to do it is to use DateInterval, of course.
I expect the following code to generate a list of 5 consecutive days starting with today.
$startDay = new DateTime();
for($i = 0; $i <=4; $i++){
$courseDay = $startDay->add(new DateInterval("P{$i}D"));
print_r($courseDay->format('j-M-Y') . "\n");
}
However it gives the following output when run today (21st October 2011):-
21-Oct-2011
22-Oct-2011
24-Oct-2011
27-Oct-2011
31-Oct-2011
I don't see anything wrong with the code, can anybody else? Why is it jumping days?
The code should be refactored as below as DateInterval::add() modifies the instantiated object and then returns the modified version of itself to allow method chaining.
I guess I should have rtm first :)
$startDay = new DateTime();
for($i = 0; $i <=4; $i++){
print_r($startDay->format('j-M-Y') . "\n");
$startDay->add(new DateInterval("P1D"));
}