I'm making a CRM-system, and I now need to offer people to make recurring events. For that they will have to fill out if it's daily, weekly, monthly or yearly, and everything like when you do it in Google Calendar.
But how will I get the dates in an array for "monday next 3 weeks" for example?
$int_count = 3; // How many to repeat
$date = new \DateTime('next monday');
$result = array($date->format('Y-m-d'));
for ($i=1; $i<$int_count; $i++) {
$result[] = $date->modify('+1 week')->format('Y-m-d');
}
print_r($result);
Result:
Array
(
[0] => 2015-01-26
[1] => 2015-02-02
[2] => 2015-02-09
)
The best way is to use DateTime and DatePeriod classes. It's the most correct way to deal with dates now. It deals with timezones and DST shifts automatically. It's just the way you must do it.
$daterange = new DatePeriod(new DateTime('next monday'), new DateInterval('P1W'), 2);
$dates = [];
foreach($daterange as $date) $dates []= $date->format("Y-m-d H:i:s");
print_r($dates);
The result will be:
Array
(
[0] => 2015-01-26 00:00:00
[1] => 2015-02-02 00:00:00
[2] => 2015-02-09 00:00:00
)
Related
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 )
first of all, I'm sorry for my english. I'm from germany.
Now my Problem:
I have a multiple array with some dates in it. I had to filter the first and the last date for every IP because I need the difference of both dates to know how much time the User on my website.
I did that and got all I need. Here is a part of my code output:
$ip_with_dates:
Array
(
[0] => Array
(
[ip] => 72.xx.xx.xx
[first_date] => 2015-10-12 00:10:15
[last_date] => 2015-10-12 01:10:51
)
[1] => Array
(
[ip] => 85.xx.xx.xx
[first_date] => 2015-10-12 00:10:19
[last_date] => 2015-10-12 01:10:56
)
I tried to get the time between those two dates with:
$visit_lenght = [];
foreach($ip_with_date as $key => $val){
$date1 = new DateTime($val['first_date']);
$date2 = new DateTime($val['last_date']);
$interval = $date1->diff($date2)->format('%h %m %s');
$visit_lenght[] = $interval;
}
what gives me this output:
Array
(
[0] => 1 36
[1] => 1 37
[2] => 0 3
[3] => 0 9
)
well but this isn't good to work with. I need the time in seconds not in H:m:s
but I really don't now how. This is a part of my project where I'm really fighting with. Maybe someone of you could help me with this.
I'm working with laravel. Normal PHP would make it too but if someone knows a solution in laravel, it would be nice as well!
thanks for any help!
To get time diff in seconds you need to convert your datetime objects to timestamps:
$visit_lenght = [];
foreach($ip_with_date as $key => $val){
$date1 = new DateTime($val['first_date']);
$date2 = new DateTime($val['last_date']);
$interval = $date1->getTimestamp() - $date2->getTimestamp()
$visit_lenght[] = $interval;
}
You may get timestamps of two dates and count the difference.
something like (in php).
$date1t = $date1->getTimestamp();
$date2t = $date2->getTimestamp();
$diff = $date2t - $date1t;
http://php.net/manual/en/datetime.gettimestamp.php
Try this way (general/basic way)
$visit_lenght = [];
foreach($ip_with_date as $key => $val){
$date1 = strtotime($val['first_date']);
$date2 = strtotime($val['last_date']);
//$interval = $date1->diff($date2)->format('%h %m %s');
$interval = $date2 - $date1;
$visit_lenght[] = $interval;
}
you can make this:
->format('Y-m-d H:i:s')
With DateTime() class of PHP, it is super simple to find difference between time. Here is an exmple:
<?php
$ip = "xxxx-xxx-xxx";
$t1 = DateTime::createFromFormat('Y-m-d H:i:s', '2015-10-12 00:10:15');
$t2 = DateTime::createFromFormat('Y-m-d H:i:s', '2015-10-12 01:10:51');
echo "User from IP {$ip} spent " . $t1->diff($t2)->format("%h hours, %i minutes and %s seconds");
?>
So I have a form for creating scheduled dates.
Title, Subject, bla bla...
But then, I have a jQuery Date picker, that lets the user pick a date off a calendar.
The jQuery date picker only formats for human dates
I want to store them in UNIX TIME.
So I have this Calendar, for the YEAR, MONTH, DAY...
Then I have a standard drop down for the hour, 1:00 PM, 1:30 PM Etc...
The post print_r($_POST); looks like this,
[time] => Array
(
[0] => 5:00 PM
[1] => 1:00 PM
[2] => 8:00 PM
)
[date] => Array
(
[0] => 2014-05-08
[1] => 2014-04-04
[2] => 2014-03-28
)
I found strtotime(); for converting, human time / date into UNIX TIME, however...
How do I get array [0] from time, and date to combine and be a combined string.
There might be only 1 date, or 8 dates?!
You can iterate through your POST data and combine times:
foreach($_POST['date'] as $i => $date) {
$timestamp = strtotime($date.' '.$_POST['time'][$i]);
}
$count = count($_POST['date']); // assuming both date and time have same amount of values
for ($i = 0; $i < $count; $i++) {
$time = strtotime($_POST['date'][$i] . ' ' . $_POST['time'][$i]);
// do what you want with the time here
// Example: put all timestamps in an array.
$timestamps[] = $time;
}
I have an array of years, months and weeks.
//Returns an array containing the years, months and week numbers between two dates
function year_month($start_date, $end_date)
{
$begin = new DateTime( $start_date );
$end = new DateTime( $end_date);
$end->add(new DateInterval('P1W')); //Add 1 week to include the end date as a week
$interval = new DateInterval('P1W'); //Add 1 week
$period = new DatePeriod($begin, $interval, $end);
$aResult = array();
foreach ( $period as $dt )
{
$aResult[$dt->format('Y')][$dt->format('M')][] = "W".$dt->format('W');
}
return $aResult;
}
echo '<pre>';
print_r(year_month("25-11-2013","26-01-2014"));
echo '</pre>';
it outputs the following:
Array
(
[2013] => Array
(
[Nov] => Array
(
[0] => W48
)
[Dec] => Array
(
[0] => W49
[1] => W50
[2] => W51
[3] => W52
[4] => W01
)
)
[2014] => Array
(
[Jan] => Array
(
[0] => W02
[1] => W03
[2] => W04
[3] => W05
)
)
)
Notice that w01 is in the Dec array instead of the January array. I assume this is because Monday is the start of each week and in this case Monday is the 30th of December. Any ideas on how to get around this? In fact I am not sure what to do with border cases in general. If a week starts in one month but ends in another it should not be inserted into both months I would rather it be in the month in which it ends. But not sure how to go about this.
Use the thursday in the week of the starting date as the starting point, before adding weeks. The thursday will always be in the correct month (since that's what we use to decide if we're going to have week 53 or week 1 in the last week of December). If that thursday is in January, you're going to get W1 and January, if it's in December, you're going to get W53 and December.
If you want the month the week ends each time, use the sunday as the starting point instead.
You assume right, week "belongs" to month in which week start date is, so your output is correct.
If you wish to set month to last day in week, this can be accomplished with setISODate() method:
foreach ($period as $dt) {
$dt->setISODate($dt->format('o'), $dt->format('W'), 7);
$aResult[$dt->format('Y')][$dt->format('M')][] = "W".$dt->format('W');
}
But this will now work for every week that has dates in 2 different months, W05 is now in February.
Demo
After more than an hour struggling and trying I'd like to ask it here.
Trying to make something with weeks etc. in php I got from you site this:
Get all Work Days in a Week for a given date
Nice and will work for me fine.
But ... I can't get, trying and trying, the data out of this part: [date] => 2013-08-12 00:00:00
Array
(
[0] => DateTime Object
(
[date] => 2013-08-12 00:00:00
[timezone_type] => 3
[timezone] => Europe/Amsterdam
)
How to get that date out of the array ?
Please help me out, thanks in advance for the help !
Use DateTime::format()
$dateTime = new DateTime('2013-08-12 00:00:00');
echo $datetime->format('Y-m-d'); // produces 2013-08-12
$firstMondayThisWeek= new DateTime('2013-08-12');
$firstMondayThisWeek->modify('tomorrow');
$firstMondayThisWeek->modify('last Monday');
$nextFiveWeekDays = new DatePeriod(
$firstMondayThisWeek,
DateInterval::createFromDateString('+1 weekdays'),
4
);
$dateTimes = iterator_to_array($nextFiveWeekDays);
foreach ($dateTimes as $dateTime) {
echo $dateTime->format('Y-m-d H:i:s');
}