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"));
}
Related
In my database I use CURRENT_TIMESTAMP to keep track of the registration dates for my new users.
Now I wish to generate a list of users who signed up in the past 7 or 30 days.
I figured it would be as simple as explained in this topic;
Get 30 days back date along with time
But my current code generates an unexpected output.
for($i = 0; $i < $userlenght; $i++){
$comparedate = strtotime(date('Y-m-d', $user[$i]["date"]));
echo $comparedate . "<br/>";
if($user[$i]["date"] > $last){
//do stuff here with the users who match.
}
}
This displays the $comparedate value as "-3600" and I have no idea why.
Can anyone provide some insight?
SQL Timestamp is returned formatted as a string, so you need to instruct PHP to read the string as a date formatted value. To do so you need to use strtotime().
Try the following:
for($i = 0; $i < $userlenght; $i++){
$comparedate = strtotime(date('Y-m-d', strtotime($user[$i]["date"])));
echo $comparedate . "<br/>";
if($user[$i]["date"] > $last){
//do stuff here with the users who match.
}
}
Your problem is that $user[$i]['date'] is a MySQL Timestamp which looks like 2018-09-14 11:09:10, and is not a valid input to date. You can just remove the call to date and your code should work fine:
$comparedate = strtotime($user[$i]["date"]);
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>';
}
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.
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'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)));
}