This question already has answers here:
PHP Carbon, get all dates between date range?
(11 answers)
Closed 4 years ago.
Good day.
Ive been trying to get the days from the current day using Carbon in Laravel. So if today is the December 20 I want to get an array with:
December 20, 2018
December 21, 2018
December 22, 2018
December 23, 2018
December 24, 2018
I want to do it for 2 weeks or maybe even 3 weeks. This is what I got so far.
$currentdate = Carbon::now()->format('m/d/Y');
$ts = strtotime($currentdate);
$year = date('o', $ts);
$week = date('W', $ts);
$datearray = [];
for($i = 1; $i <= 7; $i++) {
$ts = strtotime($year.'W'.$week.$i);
array_push($datearray,date("m/d/Y", $ts));
}
The code above gives me for the week from the starting of the week and not from the day going forward.
If you are already using Carbon, use its power!
use \Carbon\Carbon;
// how many days you want in your array
$count = 7;
$dates = [];
$date = Carbon::now();
for ($i = 0; $i < $count; $i++) {
$dates[] = $date->addDay()->format('F d, Y');
}
// Show me what you got
print_r($dates);
Output is:
Array
(
[0] => December 22, 2018
[1] => December 23, 2018
[2] => December 24, 2018
[3] => December 25, 2018
[4] => December 26, 2018
[5] => December 27, 2018
[6] => December 28, 2018
)
Try this,
$datearray = [];
for($i = 0; $i < 7; $i++) {
array_push($datearray, date('F d, Y', strtotime($i == 0 ?'today UTC':'today +'.$i.'day')));
//push to array as 'December 20, 2018'
}
enjoy coding~! :)
Related
I have created an array of the next 10 days, with a 2 days buffer (i.e. if it is a Monday, the array starts on Wednesday). I am now trying to remove weekends from my array but unsure how to go about doing this. Below is my PHP and the returned array:
$date_buffer = strtotime('+2 days');
$days = array();
for ($i = 0; $i < 10; $i++) {
$days[date($date_buffer)] = date("l, jS M", $date_buffer);
$date_buffer = strtotime('+2 days', $date_buffer);
}
print_r($days);
This returns:
Array (
[1548192409] => Tuesday, 22nd Jan
[1548365209] => Thursday, 24th Jan
[1548538009] => Saturday, 26th Jan
[1548710809] => Monday, 28th Jan
[1548883609] => Wednesday, 30th Jan
[1549056409] => Friday, 1st Feb
[1549229209] => Sunday, 3rd Feb
[1549402009] => Tuesday, 5th Feb
[1549574809] => Thursday, 7th Feb
[1549747609] => Saturday, 9th Feb
)
Can somebody help me understand how I would filter out any Saturdays or Sundays from the above
http://php.net/manual/en/function.date.php
$date_buffer = strtotime('+2 days');
$days = array();
for ($i = 0; $i < 10; $i++) {
if (!in_array(date('w',$date_buffer), [0,6])) {
$days[date($date_buffer)] = date("l, jS M", $date_buffer);
}
$date_buffer = strtotime('+2 days', $date_buffer);
}
print_r($days);
This is a good job for the DatePeriod class. We set up a period of 10 recurrences of 2 days from the start time (in 2 days), and then can iterate through the dates, checking for a weekend day (day of week = 0 or 6) to exclude them from the output:
$start = new DateTime('+2 days');
$period = new DatePeriod($start, new DateInterval('P2D'), 9);
foreach ($period as $date) {
$dow = (int)$date->format('w');
if ($dow != 0 && $dow != 6) {
$days[$date->format('U')] = $date->format('l, jS M');
}
}
print_r($days);
Output:
Array (
[1548194036] => Tuesday, 22nd Jan
[1548366836] => Thursday, 24th Jan
[1548712436] => Monday, 28th Jan
[1548885236] => Wednesday, 30th Jan
[1549058036] => Friday, 1st Feb
[1549403636] => Tuesday, 5th Feb
[1549576436] => Thursday, 7th Feb
)
If you wanted 10 consecutive days (excluding weekends) from 2 days from today, you would just change the second line of the code to:
$period = new DatePeriod($start, new DateInterval('P1D'), 9);
and the output would be:
Array (
[1548197829] => Tuesday, 22nd Jan
[1548284229] => Wednesday, 23rd Jan
[1548370629] => Thursday, 24th Jan
[1548457029] => Friday, 25th Jan
[1548716229] => Monday, 28th Jan
[1548802629] => Tuesday, 29th Jan
[1548889029] => Wednesday, 30th Jan
[1548975429] => Thursday, 31st Jan
)
Demo on 3v4l.org
Here is a simple answer using the while loop.
https://3v4l.org/0lpGX
<?php
$x = 1; // Start
$y = 10; // Iterations Needed
$days = []; //Empty Array
while($x <= $y) {
// Set Buffer
$buffer = 2 + $x;
// Get Date with Buffer
$date = date(strtotime("+$buffer days"));
// If the day is a weeday
if(date('N', $date) < 6){
// Add to array
$days[$date] = date("l, jS M", $date);
// If not, increase max iteration (example: 10 to 11)
}else{
$y++;
}
// Go to next loop
$x++;
}
echo "<pre>";
print_r($days);
?>
Which prints out
Array
(
[1548283397] => Wednesday, 23rd Jan
[1548369797] => Thursday, 24th Jan
[1548456197] => Friday, 25th Jan
[1548715397] => Monday, 28th Jan
[1548801797] => Tuesday, 29th Jan
[1548888197] => Wednesday, 30th Jan
[1548974597] => Thursday, 31st Jan
[1549060997] => Friday, 1st Feb
[1549320197] => Monday, 4th Feb
[1549406597] => Tuesday, 5th Feb
)
I have fetched a current month from my DB which is basically a join date of the user. Lets say the use joined this month and it is May. The code I do to fetch the month name is like this:
$months = array();
array_push($months,date("F",strtotime($me['joinTime'])));
In this case I add the start month to the array, which in this case is May... Now what I'd like to do is as the months go by, I'd like to add each new month to the array.. So for instance in a few days its June, and when June kicks in, I'll add that Month as well to the array.. So my question here is, how can I get the rest of the month names from the start date (May).
I need June, July, August, September, October, November, December...
If the start month was April I'd add May into the array as well...
Can someone help me out with this ?
First you need to get he month number and than you need to use a loop through to end of the year that is 12. For each month number you also need the month name so use DateTime createFromFormat.
Online Check
$months = array();
$num = date("n",strtotime($me['joinTime']));
array_push($months, date("F", strtotime('2016-05-17 16:41:51')));
for($i = ($num + 1); $i <= 12; $i++){
$dateObj = DateTime::createFromFormat('!m', $i);
array_push($months, $dateObj->format('F'));
}
print_r($months); // Array ( [0] => May [1] => June [2] => July [3] => August [4] => September [5] => October [6] => November [7] => December )
Yo can also put it like
$array = array();
array_push($array, date('F')) ;
for ($i=1; $i<= 12 - date('m'); $i++ ){
array_push($array, date('F', strtotime("+$i months"))) ;
}
print "<pre>";print_r($array);
Here we will be using DatePeriod which allows iteration over a set of dates and times, recurring at regular intervals, over a given period.
So we got the end date and we have the start date and then calculated the interval. And then looping over the period we got the array of months.
// current date : 20 Feb 2019
$startDate = new \DateTime('first day of next month');
$endDate = new \DateTime('1st january next year');
$interval = new \DateInterval('P1M');
$period = new \DatePeriod($startDate, $interval, $endDate);
// Start array with current date
$dates = [];
// Add all remaining dates to array
foreach ($period as $date) {
array_push($dates, $date->Format('F'));
}
// output
print_r($dates); die;
Array ( [0] => March [1] => April [2] => May [3] => June [4] => July [5] => August [6] => September [7] => October [8] => November [9] => December )
I had a technical test and one of the things I had to do is a program that has an input of two dates, and has to output the months with 5 sundays between these two dates. A string $dates has the two dates in the same line
example: juny 2014 october 2014
I need to use these two dates to do stuff.
In this point, I was thinking to do two separate strings to work with each date, like this:
$arrayDates = split(" ",$dates);
Then:
$firstDate = $dates[0].$dates[1];
$secondDate = $dates[2].$dates[3];
But I see this way of doing things too obvious and not dynamyc. Somebody knows a better or more elegant/apropriate way to do this?
Edit: this is the full code
<?php
date_default_timezone_set('UTC');
//output month with five sundays
foreach(file($argv[1]) as $date){
$totalDays=prepareDateArray($date);
calculateSundays($totalDays,$date);
}
function prepareDateArray(&$date){
$regexPattern = "/([a-z]+ [0-9]{4})/i";
$matchCount = preg_match($regexPattern, $date, $matches);
if($matchCount == 0)return;
$date = preg_replace("# +#",' ',$date);
$date = split(' ',$date);
$totalDays = parseDate($date);
return $totalDays;
}
function parseDate(&$date){
$nIndex = count($date);
if($nIndex%2==0){
//Add first day of the month to first Date
$date[0] = '01-'.$date[0].'-'.$date[1];
//Get lastMonthDay
$lastMonthDay = date('t', strtotime($date[2].'-'.$date[3]));
//Add last day of the month to secondDate
$date[1] = $lastMonthDay.'-'.$date[2].'-'.$date[3];
//Calculates distance between dates in days
$firstDate = strtotime($date[0]);
$secondDate = strtotime($date[1]);
if($firstDate>$secondDate)exit('First date must be earlier than second date');
$datediff = $secondDate - $firstDate;
unset($date[2],$date[3]);
return $datediff/(60*60*24);
}else{
exit('Insert valid input');
}
}
function calculateSundays($totalDays,$tempDate){
$sundays=0;
$output=0;
for($day=1;$day<=$totalDays;$day++)
{
$dayOfWeek = date('w', strtotime($tempDate[0].' + '.$day.' days'));
if($dayOfWeek == 0)$sundays++;
if($sundays == 5){
$output++;
$sundays = 0;
}
}
if($output == 0)print("Wrong date \n");
else print($output."\n");
}
?>
You can create a simple regex for this operation can you check out the code below;
<?PHP
header('Content-Type: text/plain; charset=utf8');
$regexPattern = "/([a-z]+ [0-9]{4})/i"; //This will match a letters + space + 4 digit number together
$inputString = "juny 2014 october 2014 february 2016 march 2017 july 2010 FEBRUARY 2014";
$matchCount = preg_match_all($regexPattern, $inputString, $matches);
print_r($matches);
?>
You will get an output like that;
Array
(
[0] => Array
(
[0] => juny 2014
[1] => october 2014
[2] => february 2016
[3] => march 2017
[4] => july 2010
[5] => FEBRUARY 2014
)
[1] => Array
(
[0] => juny 2014
[1] => october 2014
[2] => february 2016
[3] => march 2017
[4] => july 2010
[5] => FEBRUARY 2014
)
)
And working example is here http://ideone.com/LuQqEN
i use same as cihan-uygun code but my Regx expression is little change
here is code
example :
I need dates output from these strings
2020 Ice Harbor All-Ages Master/Expert - Nov 21, 2020
2020 Ice Harbor Scholastic Open (Nov 21, 2020 - Nov 22, 2020)
<?php $regexPattern = "/[A-Z]{3} [0-9]{2}\, [0-9]{4}/i";
preg_match_all($regexPattern, $stringgosehere, $matches); ?>
I have a number of variables that store a year, month and series of dates for that month (there are 2 of these for 2 separate months). I then need to incorporate these into what I believe is a multidimensional array (haven't worked with these types of arrays before). Here's my code that has the variables:
// Set the default timezone
date_default_timezone_set('Australia/Sydney');
$month1 = date('m');
$year1 = date('Y');
$dates1 = '3 5 6 10 12 13 17 19 20 24 26 27 31';
$month2 = date('m', strtotime('first day of next month')) ;
$year2 = date('Y', strtotime('first day of next month')) ;
$dates2 = '10 15 26';
Using Dec 10, 2013 as the current date and the above list of dates I then need to end up with an array in this format:
array("year" => array("month" => array(days)));
that would look like this:
$daysArray = array ("2013" => array("12" => array(3,5,6,10,12,13,17,19,20,24,26,27,31)), "2014" => array("1" => array(10,15,26)));
I'm not sure how to convert these 6 variables into a multidimensional array?
Considering a few edge cases as well (same year etc), i think this is a rather simple (readable) solution:
// Sample data
$month1 = 12;
$year1 = 2013;
$dates1 = '3 5 6 10 12 13 17 19 20 24 26 27 31';
$month2 = 1;
$year2 = 2014;
$dates2 = '10 15 26';
$result = combineDateArrays(createDateArray($year1, $month1, $dates1), createDateArray($year2, $month2, $dates2));
function createDateArray($year, $month, $dates) {
return array($year=>array($month=>explode(" ", $dates)));
}
function combineDateArrays($dateArray1, $dateArray2) {
foreach($dateArray2 as $year=>$months) {
foreach($months as $month=>$days) {
if (!isset($dateArray1[$year])) $dateArray1[$year] = array();
$dateArray1[$year][$month] = $days;
}
}
return $dateArray1;
}
You can create an array from a string using explode:
$daysArray = array($year1 => $month1 => explode(' ', $dates1), $year2 => $month2 => explode(' ', $dates2));
I have an array with timestamps. If this timestamps are between two given dates I need to collect them into another array.
Let me use an example:
Lets say $array1[] has:
Array ( [0] => 1299147500 [1] => 1299147453 [2] => 1299146476 [3] => 1299143220 [4] => 1297934349 [5] => 1297845742 [6] => 1297695551 [7] => 1296134251 [8] => 1295948452 [9] => 1295554308 [10] => 1295369389 [11] => 1295345559 [12] => 1295261432 [13] => 1295014784 [14] => 1294929846 [15] => 1294832875 )
I need to create $array2[] with those values from $array1[] that are between Thursday February 17, 2011 and Thursday March 3, 2011
How can I do this?
Thanks a ton
$low = strtotime('Thursday February 17, 2011');
$high = strtotime('Thursday March 3, 2011');
$array2 = array();
foreach($array1 as $timestamp) {
if ($low <= $timestamp && $timestamp <= $high) {
$array2[] = $timestamp;
}
}
an alternative using array_filter which will maintain the keys.
$low = strtotime('Thursday February 17, 2011');
$high = strtotime('Thursday March 3, 2011');
$array2 = array_filter($array1, function($timestamp) use ($low, $high) {
return $low <= $timestamp && $timestamp <= $high;
});
Convert first and last date to timestamps using strtotime()
For each item in the array, if it is between min and max, copy it to the second array.
Sort the array of timestamps
http://codepad.org/mDvRJ534
<?php
$array1 = array(1299147500,1299147453,1299146476,1299143220,1297934349,1297845742,1297695551,1296134251,1295948452,1295554308,1295369389,1295345559,1295261432,1295014784,1294929846,1294832875);
$array2 = array();
$date1 = strtotime('Thursday February 17, 2011');
$date2 = strtotime('Thursday March 3, 2011');
foreach($array1 as $timestamp){
if($timestamp <= $date2 && $timestamp >= $date1)
$array2[] = $timestamp;
}
echo 'date1 = '.$date1."\n";
echo 'date2 = '.$date2."\n";
print_r($array2);
Someone answered this in another post over here mate How to check if a date is in a given range?