I need to get 26 dates from a starting point. The next date starting from the previous one. It would be insane to hard code everything... So I was wondering how can I do this dynamically? Is there a smarter way? I'm looking to increment after the second date. Maybe with a for loop?
<?php
//incrementing dates for bi-weekly (26 periods// 26 dates)
$firstdate = strtotime("+17 days", strtotime("2017-04-03"));//1
$i = date("Y-m-d", $firstdate); echo date("Y-m-d", $firstdate);//echo for testing
echo'<br>';
$seconddate =strtotime("+14 days", strtotime($i));//2
$ii = date("Y-m-d", $seconddate); echo date("Y-m-d", $seconddate);//echo for testing
echo'<br>';
?>
How about this:
// initialize an array with your first date
$dates = array(strtotime("+17 days", strtotime("2017-04-03")));
// now loop 26 times to get the next 26 dates
for ($i = 1; $i <= 26; $i++) {
// add 14 days to previous date in the array
$dates[] = strtotime("+14 days", $dates[$i-1]);
}
// echo the results
foreach ($dates as $date) {
echo date("Y-m-d", $date) . PHP_EOL;
}
Probably the easiest way to do this would be with an array
$myDates = [];
$firstdate = strtotime("+17 days", strtotime("2017-04-03"));
array_push($myDates, date("Y-m-d",$firstdate));
for($i=0;$i<25;$i++){
$lastdate = $myDates[$i];
$nextdate = strtotime("+14 days", strtotime($lastdate));
array_push($myDates,date("Y-m-d",$nextdate));
}
echo "<pre>".var_dump($myDates)."</pre>";
Related
How to get all recurring dates for chosen week of the day?
For example, today is Wednesday 15/02/2017 and if I choose Thursday how to get next 5 dates starting next Thursday like:
$dates = array(
[0] => "16/02/2017",
[1] => "23/02/2017",
[2] => "02/03/2017",
[3] => "09/03/2017",
[4] => "16/03/2017");
If I choose Tuesday, get dates starting 21/02/2017, which is next Tuesday
You can use strtotime to generate timestamps for this dates, the date function can generate actual dates.
<?php
// Array for dates
$dates = [];
// Get next thursday
$date = strtotime('thursday');
$dates[] = $date;
// Get the next four
for ($i = 0; $i < 4; $i++)
{
$date = strtotime('+1 week', $date);
$dates[] = $date;
}
// Echo dates
foreach ($dates as $date)
echo date('Y-m-d', $date);
Take a look at this post
Using that idea:
//Get First Thursday
$date = new DateTime();
$date->modify('next thursday');
echo "<br/>Starting Thursday: " . $date->format('d/m/Y');
//Loop over next 4 Thursdays
for($i = 2; $i <= 5; $i++)
{
$date->modify('+7 days');
echo "<br/>Thursday " . $i . ": " . $date->format('d/m/Y');
}
Suppose i have a month June 2014. Now i want to get dates of all Mondays in June month.
like Monday is coming on following days so answer will be like following
2014-06-02
2014-06-09
2014-06-16
2014-06-23
2014-06-30
please do not give be static solution only for June. I need dynamic solution for every month and purely in PHP.
Try this -
<?php
$startDate = "2014-06-01";
$endDate = "2014-06-30";
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
for($i = strtotime('Monday', $startDate); $i <= $endDate; $i = strtotime('+1 week', $i))
echo date('l Y-m-d', $i).PHP_EOL;
DEMO:
http://3v4l.org/n4ULA
Try to create an array with all your date with day on key (with variable $day and $date):
$array = array("Monday" => "2014-06-02", "Tuesday" => "2014-06-03", "Wednesday" => "2014-06-04");
You create a loop to reach all the result :
foreach($array as $key => $value {
if($key == "Monday")
echo $value;
}
Using the above, I created this so you can use variables to define the month, year, and selected weekday.
$month = "6";
$year = "2022";
$weekday = "Tuesday";
$d=cal_days_in_month(CAL_GREGORIAN,$month,$year);
$first_date_of_month = $year."-".$month."-01";
$last_date_of_month = $year."-".$month."-".$d;
$startDate = strtotime($first_date_of_month);
$endDate = strtotime($last_date_of_month);
for($i = strtotime($weekday, $startDate); $i <= $endDate; $i = strtotime('+1 week', $i))
echo "<br />". date('l Y-m-d', $i).PHP_EOL;
I want a loop that checks the current month, 12 months in the future and 4 months in the past.
For example: Today is 1st August 08. My loop should go through April, May, June, July, August, September, October, November, December, January, February, March, April, May, June, July, and August.
I have tried strotime but I don't know how I can loop 4 months back and 12 months in the future.
Here is my code
$i = 1;
$month = strtotime('2013-08-01');
while($i <= 12) {
$month_name = date('F', $month);
echo $month_name;
echo "<br>";
$month = strtotime('+1 month', $month);
$i++;
I think Yoshi was almost there with his answer, but using DatePeriod with DateTime is more consistent and makes for more readable code IMHO:-
$oneMonth = new \DateInterval('P1M');
$startDate = \DateTime::createFromFormat('d H:i:s', '1 00:00:00')->sub(new \DateInterval('P4M'));
$period = new \DatePeriod($startDate, $oneMonth, 16);
foreach($period as $date){
//$date is an instance of \DateTime. I'm just var_dumping it for illustration
var_dump($date);
}
See it working
This can be quite tricky, here's how I would do it:
$month = date("n", "2013-08-01") - 1; // -1 to get 0-11 so we can do modulo
// since you want to go back 4 you can't just do $month - 4, use module trick:
$start_month = $month + 8 % 12;
// +8 % 12 is the same is -4 but without negative value issues
// 2 gives you: 2+8%12 = 10 and not -2
for ($i = 0; $i < 16; $i += 1) {
$cur_month = ($start_month + $i) % 12 + 1; // +1 to get 1-12 range back
$month_name = date('F Y', strtotime($cur_month . " months"));
var_dump(month_name);
}
something like this?:
$start = -4;
$end = 12;
for($i=$start; $i<=$end;$i++) {
$month_name = date('F Y', strtotime("$i months"));
echo $month_name;
echo "<br>";
}
Your code, just slightly modified.
date_default_timezone_set('UTC');
$i = 1;
$month = strtotime('-4 month');
while($i <= 16) {
$month_name = date('F', $month);
echo $month_name;
echo "<br>";
$month = strtotime('+1 month', $month);
$i++;
}
Simplest solution:
for($i=-4; $i<=12; $i++) {
echo date("F",strtotime( ($i > 0) ? "+$i months" : "$i months") )."\n";
}
Explanation:
The loop starts at -4 and goes all the way upto 12 (total 17, including 0). The ternary statement inside strtotime() simply checks if $i is positive, and if it is, a + is inserted so that we'll get the results for strtotime("+1 months") and similar.
Ta-da!
Using DateTime is the easiest and more readable way.
I would do it like this:
$from = new DateTime('-4 month');
$to = new DateTime('+12 month');
while($from < $to){
echo $from->modify('+1 month')->format('F');
}
Is it possible to get the weekday of the 21st of the next 6 months in PHP?
For example, say the 21st falls on a Tuesday next month, then I want "Tuesday" to be returned. But I want this for each of the next 6 months. What is the most elegant solution to this?
Something like that will give you the expected result:
// starting date
$date = new DateTime('2012-08-21');
// iterate for 6 months
for ($i = 0; $i < 6 ; $i++) {
echo $date->format('Y-m-d').' : '.$date->format('l') . PHP_EOL;
$date->modify('+1 month');
}
The DatePeriod and DateInterval classes are super-handy for this sort of thing.
$date = DateTime::createFromFormat('d', 21);
$period = new DatePeriod($date, new DateInterval('P1M'), 6, DatePeriod::EXCLUDE_START_DATE);
foreach ($period as $day) {
echo $day->format('M jS => l'), PHP_EOL;
}
Yes:
<?php
$date = new DateTime;
$date->modify("first day of this month");
$date->modify("+20 days"); //21st
echo $date->format("F: l (Y-m-d)") . PHP_EOL;
for ($i = 0; $i < 6; $i++) {
$date->modify("+1 month");
echo $date->format("F: l (Y-m-d)") . PHP_EOL;
}
Untested, but try this:
$date = 21;
$year = 2012;
$month = date('m'); // Get current month
for($i=0;$i<6;$i++)
{
if($month == 13)
{
$year++;
$month=1;
}
$day = date('l',mktime(0,0,0,$month,$date,$year));
echo "$month $date falls on $day<br />\n";
$month++;
}
I am trying to create an array starting with today and going back the last 30 days with PHP and I am having trouble. I can estimate but I don’t know a good way of doing it and taking into account the number of days in the previous month etc. Does anyone have a good solution? I can’t get close but I need to make sure it is 100% accurate.
Try this:
<?php
$d = array();
for($i = 0; $i < 30; $i++)
$d[] = date("d", strtotime('-'. $i .' days'));
?>
Here is advance latest snippet for the same,
$today = new DateTime(); // today
$begin = $today->sub(new DateInterval('P30D')); //created 30 days interval back
$end = new DateTime();
$end = $end->modify('+1 day'); // interval generates upto last day
$interval = new DateInterval('P1D'); // 1d interval range
$daterange = new DatePeriod($begin, $interval, $end); // it always runs forwards in date
foreach ($daterange as $date) { // date object
$d[] = $date->format("Y-m-d"); // your date
}
print_r($d);
Working demo.
Official doc.
For those who want to show sales of the past X days,
As asked in this closed question (https://stackoverflow.com/questions/11193191/how-to-get-last-7-days-using-php#=), this worked for me.
$sales = Sale::find_all();//the sales object or array
for($i=0; $i<7; $i++){
$sale_sum = 0; //sum of sale initial
if($i==0){
$day = strtotime("today");
} else {
$day = strtotime("$i days ago");
}
$thisDayInWords = strftime("%A", $day);
foreach($sales as $sale){
$date = strtotime($sale->date_of_sale)); //May 30th 2018 10:00:00 AM
$dateInWords = strftime("%A", $date);
if($dateInWords == $thisDayInWords){
$sale_sum += $sale->total_sale;//add only sales of this date... or whatever
}
}
//display the results of each day's sale
echo $thisDayInWords."-".$sale_sum; ?>
}
Before you get angry:
I placed this answer here to help someone who was directed here from that question. Couldn't answer there :(
You can use time to control the days:
for ($i = 0; $i < 30; $i++)
{
$timestamp = time();
$tm = 86400 * $i; // 60 * 60 * 24 = 86400 = 1 day in seconds
$tm = $timestamp - $tm;
$the_date = date("m/d/Y", $tm);
}
Now, within the for loop you can use the $the_date variable for whatever purposes you might want to. :-)
$d = array();
for($i = 0; $i < 30; $i++)
array_unshift($d,strtotime('-'. $i .' days'));