foreach array in SQL, print as json [duplicate] - php

I want to run a while(or any) loop to output a small list of dates as an array
$start = $day = strtotime("-1 day");
$end = strtotime('+6 day');
while($day < $end)
{
echo date('d-M-Y', $day) .'<br />';
$day = strtotime("+1 day", $day) ;
}
This works fine for printing, but I want to save it as an array (and insert it in a mysql db).
Yes! I don't know what I'm doing.

to create a array, you need to first initialize it outside your loop (because of variable scoping)
$start = $day = strtotime("-1 day");
$end = strtotime('+6 day');
$dates = array(); //added
while($day < $end)
{
$dates[] = date('d-M-Y', $day); // modified
$day = strtotime("+1 day", $day) ;
}
echo "<pre>";
var_dump($dates);
echo "</pre>";
you can then use your dates using either foreach or while
foreach approach :
foreach($dates as $date){
echo $date."<br>";
}
while approach :
$max = count($dates);
$i = 0;
while($i < $max){
echo $dates[$i]."<br>";
}

$arr = Array();
while(...) {
$arr[] = "next element";
...
}
The [] adds a new element to an array, just like push() but without the overhead of calling a function.

The simple way is just:
$start = $day = strtotime("-1 day");
$end = strtotime('+6 day');
$arr = array();
while($day < $end)
{
$arr[] = date('d-M-Y', $day);
$day = strtotime("+1 day", $day) ;
}
// Do stuff with $arr
the $arr[] = $var is the syntax for appending to an array in PHP. Arrays in php do not have a fixed size and therefore can be appended to easily.

Related

Inserting dates within an array with PHP

I am getting the starting and ending dates from a form.
I need to put within an array all the date between the former two, including themselves.
I'm using a normal for loop and, at the same time, printing the result, to verify.
Everything appear to be alright.
But when I print_r the very array, I get only a series of equal dates. Which are all the same: last date + 1.
This is the code:
$date1 = date_create("2013-03-15");
$date2 = date_create("2013-03-22");
$diff = date_diff($date1, $date2);
echo $diff->format("%R%a days");
$diffDays = $diff->days;
echo $diffDays;
$dates = array();
$addDay = $date1;
for ($i = 0; $i < $diffDays; $i++) {
$dates[$i] = $addDay;
date_add($addDay, date_interval_create_from_date_string("1 day"));
echo "array: " . $i . " : " . date_format($dates[$i], 'Y-m-d');
}
print_r($dates);
PHP code demo
<?php
$dates = array();
$datetime1 = new DateTime("2013-03-15");
$datetime2 = new DateTime("2013-03-22");
$interval = $datetime1->diff($datetime2);
$days = (int) $interval->format('%R%a');
$currentTimestamp = $datetime1->getTimestamp();
$dates[] = date("Y-m-d H:i:s", $currentTimestamp);
for ($x = 0; $x < $days; $x++)
{
$currentTimestamp = strtotime("+1 day", $currentTimestamp);
$dates[] = date("Y-m-d H:i:s", $currentTimestamp);
}
print_r($dates);
I would do it that way
$startDate = new \DateTime("2017-03-15");
$endDate = new \DateTime("2017-03-22");
$dates = [];
$stop = false;
$date = $startDate;
while(!$stop){
$dates[] = $date->format('Y-m-d'); // or $dates[] = $date->format('Y-m-d H:i:s')
$date->modify('+1 day');
if($date->getTimestamp() > $endDate->getTimestamp()){
$stop = true;
}
}
print_r($dates);

Apply all year's dates as keys into an array

I have an array with 364 values. Each value of the array represents a status of each day of the year.
The array looks like this:
array([0]=>'something',
[1]=>'something_else'
....
[364]=> 'the_end_of the year')
What i want to do is to replace all the array keys with the dates of the year. Something that looks like this:
array([2015-01-01]=>'something',
[2015-01-02]=>'something_else'
....
[2015-12-31]=> 'the_end_of the year')
You can try with -
$begin = new DateTime('2015-01-01');
$end = new DateTime('2015-12-31');
$end = $end->modify('+1 day');
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$new = array();
foreach($daterange as $date){
$new[$date->format("Ymd")] = "Your values";
}
Probably you can use something like this.
<?php
date_default_timezone_set('UTC');
// Start date
$date = '2015-1-1';
// End date
$end_date = '2015-12-31';
$result = array();
while (strtotime($date) <= strtotime($end_date)) {
$date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
$result[$date]="your info";
}
echo "<pre>";print_r($result);
?>
ok here is an example
<?php
$new = array();
for($i=0; $i<365; $i++) {
$key = date('Y-m-d',strtotime('01.01.2015 +'.$i.' days'));
$new[$key] = $i;
}
var_dump($new);
die;
and in your case
$new = array();
foreach($array as $i=>$value) {
$key = date('Y-m-d',strtotime('01.01.2015 +'.$i.' days'));
$new[$key] = $value;
}
var_dump($new);
die;
You can iterate thru array and convert current day to a DateTime object using createFromFormat method like this
$d = DateTime::createFromFormat('z Y', '110 2015');
where 110 is the day of the year and 2015 is year against which to create the date to. From there you should be able to set the keys to whatever format you please.

php check multiple dates in array are within a date range

I have an array structured like this:
Array ( [0] => 24-12-2013 [1] => 25-12-2013 [2] => 26-12-2014 [3] => 27-12-2013 [4])
I would like to check if any of the dates in the array are within a given date range.
The date range is structured like this:
$start = (date("d-m-Y", strtotime('25-12-2013')));
$end = (date("d-m-Y", strtotime('26'12'2013')));
I would like to know which dates in the array are within the date range.
Couple things:
Use timestamps or DateTime objects to compare dates, not strings
Use date format YYYY-MM-DD to avoid potential ambiguity about your date format (d/m/y or m/d/y)
This code will do what you want:
$dates = array("2013-12-24","2013-12-25","2014-12-24","2013-12-27");
$start = strtotime('2013-12-25');
$end = strtotime('2013-12-26');
foreach($dates AS $date) {
$timestamp = strtotime($date);
if($timestamp >= $start && $timestamp <= $end) {
echo "The date $date is within our date range\n";
} else {
echo "The date $date is NOT within our date range\n";
}
}
See it in action:
http://3v4l.org/GWJI2
$dates = array ('24-12-2013', '25-12-2013', '26-12-2014', '27-12-2013');
$start = strtotime('25-12-2013');
$end = strtotime('26-12-2013');
$inDateRange = count(
array_filter(
$dates,
function($value) use($start, $end) {
$value = strtotime($value);
return ($value >= $start && $value <= $end);
}
)
);
<?php
$start = DateTime::createFromFormat('d-m-Y', '25-12-2013');
$end = DateTime::createFromFormat('d-m-Y', '26-12-2013');
$dates = array('24-12-2013','25-12-2013','26-12-2014','27-12-2013');
$matches = array();
foreach ($dates as $date) {
$date2 = DateTime::createFromFormat('d-m-Y', $date);
if ($date2 >= $start && $date2 =< $end) {
$matches[] = $date;
}
}
print_r($matches);
See it in action
$_between = array();
$start = date('Ymd', strtotime($start));
$end = date('Ymd', strtotime($end));
foreach ($dates as $date)
{
$date = date('Ymd',strtotime($date));
if ($date > $start && $date < $end) {
array_push($_between,$date);
continue;
}
}
echo '<pre>';
var_dump($_between);
echo '</pre>';
Loop over the array turning each date into unix time (seconds since Jan 1, 1970), and do simple math to see if the number of seconds is between the range. Like so:
$start = strtotime('25-12-2013');
$end = strtotime('26'12'2013');
foreach($date in $dates) {
$unix_time = strtotime($date);
if($unix_time > $start && $unix_time < $end)
//in range
}
// PHP >= 5.3:
$dates_in_range = array_filter($array, function($date) {
global $start;
global $end;
return (strtotime($date) >= strtotime($start) and strtotime($date) <= strtotime($end));
});

Date and number - create array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Generate incrementing date strings
I have:
$start_date = '2012-09-03';
$number_days = 5;
I would like receive array with this dates:
$dates = array(
'2012-09-03',
'2012-09-04',
'2012-09-05',
'2012-09-06',
'2012-09-07'
);
What is the best way for this?
$start_date = '2012-09-03';
$dates[] = $start_date;
$number_days = 5;
for ($i=1; $i < $number_days; $i++) {
$dates[] = date('Y-m-d', strtotime("$start_date +$i days"));
}
http://php.net/manual/en/function.date-add.php
Loop over and add the dates you need.
Have you tried something like this
function get_days($start_date, $max){
$ts=strtotime($start_date);
$next_day_interval=24*60*60;
$arr=array();
$arr[]=$start_date;
for($i=1;$i<=$max;$i++){
$ts += $next_day_interval;
$arr[]=date('Y-m-d', $ts);
}
return $arr;
}
Just wrote it here so there might be some compile time errors but I hope you get the idea.
Here's what you're searching for (work also on PHP < 5.3)
<?php
$start_date = '2012-09-03';
$number_days = 5;
$stdate = date(strtotime($start_date));
$dates = array();
for($i = 0 ; $i < $number_days ; $i++) {
$dates[$i] = date('Y-m-d', $stdate) ;
$stdate += 24*60*60;
}
var_dump($dates);
?>
Try this.
$start_date = '2012-09-03';
$number_days = 5;
$dates = array();
$TS = strtotime($start_date);
$dates[0] = $start_date;
for($i=1;$i<5;$i++)
{
$dates[$i] = date('Y-m-d', strtotime('+1 day', $TS));
$TS = strtotime($dates[$i]);
}
Modified Code from #vinay to print actual output
<?php
$start_date = '2012-09-03';
$number_days = 5;
$dates = array();
$TS = strtotime($start_date);
for($i=0;$i<5;$i++)
{
$dates[$i] = date('Y-m-d', strtotime('+1 day', $TS));
$TS = strtotime($dates[$i]);
echo date('y-m-d',$TS).'<br>';
}
?>
Here is simple example with PHP 5.3 DateTime and DateInterval. This is clear solution. Note: PHP 5.2 supports DateTime, but not DateInterval. You may declare it in custom class in PHP 5.2, see here: DateInterval Definition.
<?php
$start_date = '2012-09-03';
$number_days = 5;
$dt = new DateTime($start_date);
$dates = array();
for($i = 0; $i < $number_days; $i++) {
$dates[] = $dt->format("Y-m-d");
$dt->add(new DateInterval("P1D"));
}
print_r($dates);
?>

How to make a date limit in php?

How can I limit this date or print this date from the star_date to end_date?
ex.
$start_date="2011-05-15";//june 15 2011
$end_date="2011-07-30";//july -7,2011
The result should be.
$dates[]="2011-05-15";
$dates[]="2011-05-16";
$dates[]="2011-05-17";
$dates[]="2011-05-18";
$dates[]="....";
$dates[]="....";
$dates[]="....";
$dates[]="....";
until it reaches.
$dates[]="2011-07-30";
I would suggest take the start date as an object and keep adding 1 day (http://www.php.net/manual/en/datetime.add.php) in a loop until you reach the end date.
<?php
$start_date = new DateTime('2011-05-15');
$end_date = new DateTime('2011-07-30');
while($end_date > $start_date)
{
echo $start_date->format('Y-m-d') . "\n";
$start_date->add(new DateInterval('P1D'));
}
?>
The above code has not been tested.
This could do it ...
$start_date = strtotime('2011-05-15'); //june 15 2011
$end_date = strtotime('2011-07-30'); //july -7,2011
$dates = array();
for ($i=$start_date; $i<=$end_date; $i+=86400) {
$dates[] = date('Y-m-d',$i);
}
$start_date="2011-05-15";
$end_date="2011-07-30";
$date=$start_date;
while (strtotime($new_date) != strtotime($end_date))
{
echo $new_date=date("Y-m-d",strtotime("+1 day", strtotime($date)))."<br>";
$dates[]=$new_date;
$date=$new_date;
}
And another....
date_default_timezone_set('America/Los_Angeles');
$startDate='2011-05-15';
$endDate='2011-07-30';
$t1=strtotime($startDate);
$days=(strtotime($endDate)-$t1)/86400;
for($i=0;$i<=$days;$i++) $dates[]=date('Y-m-d',$t1+($i*86400));
print_r($dates);
And for the 'not very efficient but will do in most cases oneliner':
for($t=strtotime($startDate);$t<=strtotime($endDate);$t+=86400) $dates[]=date('Y-m-d',$t);
And one for those who know, the last for this mornings exercises:
$dates=array_map(create_function('$t','return date("Y-m-d",$t);'),range(strtotime($startDate),strtotime($endDate),86400));
This works:
$start_date = "2011-05-15";
$end_date = "2011-07-07";
$dates = array();
$stop = strtotime($end_date);
for($i = strtotime($start_date); $i <= $stop; $i += 86400)
$dates[] = date('Y-m-d', $i);
PS. I changed your July date to 07-07 to match up with your comment.
$start_date = strtotime('2011-05-15');
$end_date = strtotime('2011-07-30');
$dates = array();
for ($i = $start_date; $i<=$end_date; $i+= (strtotime('+1 day') - strtotime('now'))) {
$dates[] = date('Y-m-d',$i);
}

Categories