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.
Related
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.
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);
I want to make for loop in php pdo that will create me an json data but loop must be for an month.
I write this:
try {
for ($i=1; $i<=30; $i++) {
$temp = array();
$temp['ID'] = $i;
$output['data'][] = $temp;
}
$jsonTable = json_encode($output);
SO this return me 30 rows, for 30 days. Now I want to create a range etc. to make me a rows from 01.02.2014 to rest of month 28/29.02.2014 so February ...
How to make this possible? Some ideas?
If you can't read the manual for yourself:
$dateString = '01.02.2014';
$dt = new DateTime($dateString);
$daysInMonth = $dt->format('t');
will give you the number of days in the month specified in $dateString
One way to loop through the days in a month:
$dateString = '01.02.2014';
$startDate = new DateTime($dateString);
$period = new DateInterval('P1M');
$endDate = clone $startDate;
$endDate->add($period);
$dayPeriod = new DateInterval('P1D');
while ($startDate < $endDate) {
echo $startDate->format('Y-m-d'), PHP_EOL;
$startDate->add($dayPeriod);
}
Another way to get that list of dates for a month
$dateString = '01.02.2014';
$startDate = new DateTime($dateString);
$period = new DateInterval('P1M');
$endDate = clone $startDate;
$endDate->add($period);
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($startDate, $interval ,$endDate);
foreach($daterange as $date){
echo $date->format("Y-m-d") . PHP_EOL;
}
$startDate = "2014-03-01";
$endDate= "2014-05-25";
Result required: March, April, May;
for that PHP delivers the DatePeriod object. Just have a look at the following example.
$period = new DatePeriod(
new DateTime('2014-03-01'),
DateInterval::createFromDateString('1 month'),
new DateTime('2014-05-25')
);
foreach ($period as $month) {
echo strftime('%B', $month->format('U'));
}
A quick solution is to parse each day and check it month:
$startDate = "2014-03-01";
$endDate = "2014-05-25";
$start = strtotime($startDate);
$end = strtotime($endDate);
$result = array();
while ( $start <= $end )
{
$month = date("M", $start);
if( !in_array($month, $result) )
$result[] = $month;
$start += 86400;
}
print_r($result);
I believe it can be done much efficient by new OOP (DateTime object) approach, but this is fast and no-brain if you need to make it work.
<?php
$startDate = "2014-03-01";
echo date('F',strtotime($startDate));
?
$date = date('F',strtotime($startDate));
For full month representation (ie Januaray, February, etc)
$date = date('M',strtotime($startDate));
For abbreviated...(ie Jan, Feb, Mar)
REFERENCE
If you wanna echo out those months in between based on two dates....
$d = "2014-03-01";
$startDate = new DateTime($d);
$endDate = new DateTime("2014-05-01");
function diffInMonths(DateTime $date1, DateTime $date2)
{
$diff = $date1->diff($date2);
$months = $diff->y * 12 + $diff->m + $diff->d / 30;
return (int) round($months);
}
$t = diffInMonths($startDate, $endDate);
for($i=0;$i<$t+1;$i++){
echo date('F',strtotime($d. '+'.$i.' Months'));
}
PHP SANDBOX EXAMPLE
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);
}