For loop involving a range of dates - php

I have a date range in which I want to process between each day. So for example between
2013-03-01 00:00:00 and 2013-04-01 00:00:00 there are 31 days
so my for loop is something like this
$date_next = $date_from;
for($i=0;$i<31-1;$i++)
{
$date_next_str = new DateTime($date_next);
$date_next_1_str = new DateTIme($date_next);
$date_next_1_str->modify("+1 day");
$date_next_1_str->modify("-1 second");
$date_next_1_str->modify("+1 day");
$date_next = $date_next_1_str->date;
}
so in my first loop it will be from 2013-03-01 00:00:00 to 2013-03-01 23:59:59
However when I assign $date_next_1_str->date to $date_next at the end of the for loop, the $date_next still shows 2013-03-01 23:59:59, which was supposed to be 2013-03-02 00:00:00.
Anyone can help me with this? Thanks in advance!

You can do this quite easily using PHP's DateTime, DateInterval and DatePeriod objects:-
$startDate = \DateTime::createFromFormat('Y-m-d H:i:s', '2013-03-01 00:00:00');
$endDate = \DateTime::createFromFormat('Y-m-d H:i:s', '2013-04-01 00:00:00');
$interval = new \DateInterval('P1D');
$endDate->add($interval); //As otherwise last day will be missed off.
$period = new \DatePeriod($startDate, $interval, $endDate);
foreach($period as $date){
//each $date is an instance of DateTime
var_dump($date); // Or whatever you want to do with the DateTime object
}

Related

PHP convert the day date with the time

I am working on my php script to set up the date with the time. I need some help with convert the day date to the current day and next day date, example: my current time is 15:27 and my current date is 27-11-2019 so when I have the string for the variable get_time1 is 06:00:00, I want to convert it to 28-11-2019 06:00:00. When I have the variable get_time2 that have the time which it is 23:00:00 as my current time is before 23:00:00 so i want to convert the date with the current date with the time to 27-11-2019 23:00:00.
Code:
<?php
$get_time1 = '06:00:00';
$get_time2 = '23:00:00';
date_default_timezone_set('Europe/London');
$Date = date('Y-m-d');
$time = date('H:i:s');
?>
Can you please show me an example how I can set up the day date with the time 06:00:00 and 23:00:00 as if the time 06:00:00 is after 12am to set up the next day date and if the time 23:00:00 is before 12am then set up the time with the current date?
Thank you.
This just creates a DateTime object from the time (which will default it to todays date) and if this is less than the current date and time, it adds 1 day...
$date = DateTime::createFromFormat("H:i:s", $get_time2);
if ( $date < new DateTime() ) {
$date->modify("+1 day");
}
which gives
2019-11-27 23:00:00
and for $get_time1...
2019-11-28 06:00:00
If you use a DateTime that will allow you to do date arithmetic.
$now = new DateTime();
$tomorrow = $now->modify("+1 day");
You can also use strtotime to get a unix timestamp as explained in this answer.
$tomorrow = strtotime('+1 day');
maybe this will do?
$offset = timezone_offset_get( timezone_open( "Europe/London" ), new \DateTime() );
echo 'in London' . gmdate('d-m-Y H:i:s', date( "U" )+$offset);
echo 'current location: ' . date('d-m-Y H:i:s', date( "U" ));

how to set 00:00:00 for DateTime

What I want to get are
today's 00:00:00
week start day's 00:00:00
month start day's 00:00:00
year start day's 00:00:00
So I write the code,
$today = new \DateTime();
$weekStart = new \DateTime();
$monthStart = new \DateTime();
$yearStart = new \DateTime();
$weekStart->modify('last sunday');
$monthStart->modify('first day of this months');
$yearStart->modify('first day of this year');
print $today->format('Y-m-d h:i:s')."\n";
print $weekStart->format('Y-m-d h:i:s')."\n";
print $monthStart->format('Y-m-d h:i:s')."\n";
print $yearStart->format('Y-m-d h:i:s')."\n";
It gave me the result:
2018-11-13 09:34:02
2018-11-11 12:00:00
2018-11-01 09:34:02
2018-11-01 09:34:02
I have two questions.
How can I make each DateTime 00:00:00??
How can I get the first day of year???
PHP's date parsing is somewhat tricky. Here's the code snippet which does what you want:
$today = new \DateTime('midnight');
$weekStart = new \DateTime('midnight last sunday');
$monthStart = new \DateTime('midnight first day of this month');
$yearStart = new \DateTime('midnight first day of january this year');
echo $today->format('Y-m-d H:i:s')."\n";
echo $weekStart->format('Y-m-d H:i:s')."\n";
echo $monthStart->format('Y-m-d H:i:s')."\n";
echo $yearStart->format('Y-m-d H:i:s')."\n";
DateTime value can be set upon construction; there's no need to call DateTime::modify().
Add midnight to get the time set to 00:00:00.
first day this year doesn't work in PHP, you need to use first day of january this year instead.
Reference: PHP Documentation of relative date formats.
you can try:
$date->setTime(0, 0, 0);
$date = new DateTime('2012-07-22');
echo $date->format('Y-m-d 00:00:00');
NO need to create a object of datetime(), use above code to get current date with time 00:00:00

PHP zero out the time for a date and add 3 days

I'm looking for an elegant/efficient way to take out the time portion of a datetime in format 'Y-m-d H:i:s', and then add 3 days.
Currently the solution is:
date('Y-m-d 00:00:00', strtotime("+3 days", strtotime('2017-01-23 05:32:12')));
where 2017-01-23 05:32:12 is the date, and this correctly outputs 2017-01-26 00:00:00.
It just feels like there has to be a better way to do this.
Thanks
DateTime() offers several of ways to do this. None of them are any less verbose than your current method:
// Plain old DateTime()
$date = (new DateTime('2017-01-23 05:32:12'))->modify('+3 days')->format('Y-m-d 00:00:00');
// DateTme using DateInterval to add three days
$date = (new DateTime('2017-01-23 05:32:12'))->add(new DateInterval('P3D'))->format('Y-m-d 00:00:00');
// DateTime setting the date to midnight instead of using 00:00:00
$date = (new DateTime('2017-01-23 05:32:12'))->modify('+3 days')->modify('midnight')->format('Y-m-d H:i:s');
If the date is today you can shorten this a bit:
$date = (new DateTime('+3 days'))->format('Y-m-d 00:00:00');
you can use DateTime class
$date = new DateTime('2017-01-23 05:32:12');
$date->modify('+3 days');
$outputDateString = $date->format('Y-m-d 00:00:00');
one line:
$date = ( new DateTime('2017-01-23 05:32:12'))->modify('+3 days')->format('Y-m-d 00:00:00');

Find Dates Before Specified Date and Insert Them Using PHP & SQL

I have a date outputted from the database:
$deadline = $row['DEADLINE'];
When I print $deadline it returns: 2015-05-03 18:00:00
Now I want a way to find each day 24 hours before up until todays date and then use each of those values to insert in a new table in the database.
So in this case I want:
2015-05-03 18:00:00
2015-05-02 18:00:00
2015-05-01 18:00:00
2015-04-30 18:00:00
2015-04-29 18:00:00
To be inserted into the database 5 new rows. So I know I need todays date:
$now = date("Y-m-d H:i:s");
Then I have the date of the deadline:
$deadline = $row['DEADLINE'];
I can then find the difference in days:
$dateDiff = ($deadline - $now)/(24*60*60);
This gives me a figure (in this case it will be 5). Then could I use this figure to display the date and time for 5 days prior?
Not sure how I would insert them all though. Your help would be appreciated.
Use DateTime object, really easiest way :
PHP :
$now = new datetime();
$sub = new datetime();
for($i=0;$i<5;$i++)
{
$sub->sub(new DateInterval('P1D'));
echo $sub->format('Y-m-d H:i:s').'<br />';
echo $sub->diff($now)->format('%a days').'<br />';
}
OUTPUT :
2015-04-29 14:18:09
1 days
2015-04-28 14:18:09
2 days
2015-04-27 14:18:09
3 days
2015-04-26 14:18:09
4 days
2015-04-25 14:18:09
5 days
This isent perfect, but it will probably get you started
Try Using Datetime http://php.net/manual/en/class.datetime.php
Or Carbon/Carbon Package: https://github.com/briannesbitt/Carbon
$datetime = new DateTime("2015-04-03 18:00:00");
$now = new DateTime('NOW');
$difference = $datetime->diff($now);
echo '<pre>';
for($i = 1; $i <= $difference->days; $i++)
{
if($difference->invert){
var_dump (
$temp = $datetime
->modify('-1 day')
->format('Y-m-d H:i:s')
);
}else{
var_dump (
$temp = $datetime
->modify('+1 day')
->format('Y-m-d H:i:s')
);
}
}

PHP get dates between two dates not working as expected

I am trying to get all dates between two dates using DatePeriod class. Its working fine when the dates inputed are of the same month, but not returning all dates when the two dates are of different months.
If the dates are say 2013-06-27 and 2013-07-05 its only returning 2013-06-27, 2013-06-28, 2013-06-29, 2013-06-30. Its not giving the rest of the dates.
CODE
$begin = new DateTime($start);
$last = new DateTime($end);
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $last);
I cannot reproduce the behavior
<?php
$start = '2013-06-27';
$end = '2013-07-05';
$begin = new DateTime($start);
$last = new DateTime($end);
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $last, DatePeriod::EXCLUDE_START_DATE);
echo 'phpversion: ', phpversion(), "\n";
foreach ( $period as $dt ) {
echo $dt->format("l Y-m-d H:i:s"), "\n";
}
echo "done.\n";
prints
phpversion: 5.4.7
Friday 2013-06-28 00:00:00
Saturday 2013-06-29 00:00:00
Sunday 2013-06-30 00:00:00
Monday 2013-07-01 00:00:00
Tuesday 2013-07-02 00:00:00
Wednesday 2013-07-03 00:00:00
Thursday 2013-07-04 00:00:00
done.
Which version of php do you use?

Categories