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;
}
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 )
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
)
I have a list of dates, some with times:
01/03/2014
02/01/2014 02:00 PM
02/02/2014 1:00 PM
03/01/2014
I am going through all of these and turning them into new DateTime objects, sending them to JavaScript, then using moment.js to display them.
moment(data.date).format('L LT')
This displays the dates in the following format:
01/03/2014 12:00 AM
02/01/2014 02:00 PM
02/02/2014 01:00 PM
03/01/2014 12:00 AM
Is there a way to detect, without regex (or whatever string manipulation), that the DateTime constructor was called with just a date (without a time)?
I don't think DateTime class provides a way to detect if the object was actually created from just a date. That information is lost once the object is created. A small-work around is to use date_parse() to check for absent date time fields (as #salathe mentioned in the comments).
If the DateTime doesn't contain a hour, minute or second part, then it means it only has a date part in it. Using the above logic, your code would look like:
$datetimes = [
'01/03/2014',
'02/01/2014 02:00 PM',
'02/02/2014 1:00 PM',
'03/01/2014'
];
$withTime = array();
$withoutTime = array();
foreach ($datetimes as $dt) {
$parts = date_parse($dt);
if ($parts['hour'] === FALSE &&
$parts['minute'] === FALSE &&
$parts['second'] === FALSE) {
$withoutTime[] = $dt;
} else {
$withTime[] = $dt;
}
}
print_r($withTime);
print_r($withoutTime);
Outputs:
Array
(
[0] => 02/01/2014 02:00 PM
[1] => 02/02/2014 1:00 PM
)
Array
(
[0] => 01/03/2014
[1] => 03/01/2014
)
Demo
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
I have some date, like:
20 November 06:10
12 November 08:12
10 October 13:23
There all in the past 6 months, Now I want to strtotime() them, but they are all un complete (lack of year), how to make some process so that I could strtotime() them?
Try this:
$dates = array("10 October 13:23", "12 November 08:12", "10 October 13:23");
foreach($dates as $d){
$exploded = explode(" ", $d);
$newDate = array_slice($exploded, 0,2,true)+array(2=>"2012")+array(3 => $exploded[2]);
//print_r($newDate);
$time = strtotime(implode($newDate));
echo $time."<br/>";
}
The output i got is:
1349868180
1352704320
1349868180
The logic is:
You lack the year, so I exploded the dates into an array to slice them, insert the year (the +array(2=>"2012") part) and glue them again with implode, and then run the strtotime.
This work only for this year, so you can use this logic to add the year to all your dates, or in the future there will be absolutely no way to filter dates from different years.
I added the dates into an array for loop through all of them, you can use the loop other ways, depending on where you have all your dates stored. For example if they are in a database you can include the script in the while($row = mysqli_fetch_assoc($result)) part where $d would be $row['date'] instead.
You should use the DateTime class and its createFromFormat and getTimeStamp methods instead of strtotime.
print_r(date_parse_from_format("d F H:i", '20 November 06:10'));
gives you:
Array
(
[year] =>
[month] => 11
[day] => 20
[hour] => 6
[minute] => 10
[second] => 0
[fraction] =>
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] =>
)