Convert an array of string dates to timestamps [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a form with an input box named "date[]" and a button that allows you to create more rows to add more dates, all with the name "date[]". The date picker format in my text input box is m-d-Y (03-15-2015). When I submit the form I need to convert the array of dates to mysql Date format Y-m-d (2015-03-15). Iv successfully captured the array of dates in an array in my php script and I'm trying to do a foreach loop and convert each date[]value into a timestamp using 'strtotime', that i will then convert to the correct Y-m-d format. The problem is strtotime expects a string not an array.
My question is how can I go about iterating through each date[] value and converting it into a timestamp? There is not a fixed amount of dates, it can change with each user.
Thank you in advance.

You can do that with array_map() or array_walk()
Here's an array_walk example that let's you specify the formats at call-time.
$dates = ['03-15-2015', '05-29-2015'];
array_walk($dates, function(&$date, $i, $formats)
{
$datetime = DateTime::createFromFormat($formats['from'], $date);
$date = $datetime->format($formats['to']);
}, ['from' => 'm-d-Y', 'to' => 'Y-m-d']);

Use foreach :
$dates = $_POST['date']; // in case you are using POST request
foreach($dates as $date){
$datetime = DateTime::createFromFormat('m-d-Y', $date);
$cDates[] = $datetime->format('Y-m-d');
}
Now all the converted dates exist inside $cDates array.

Related

Laravel where condition on date column haves a date range string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a column name(available_on) which haves a value in string ('11/10/2020 - 11/14/2020') and now i want to apply condition where current date exists in between this string i.e between 11/10/2020 And 11/14/2020
And I want to do this in mysql query.
You can give also a solution in laravel or php
// if available_on is from a object named $data so :
$availableOn = explode(" - ", $data->available_on); // split two date
$dateFrom = strtotime($availableOn[0]); // convert into unix_timestamp
$dateTo = strtotime($availableOn[1]);
if( time() > $dateFrom AND time() < $dateTo ){ // check if current time() between two
return true; // it's in available time
}else{
return false; // it's not in this time
}

How can i convert days names to dates of the current month? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an array of days in a month excluding friday and sunday as they aren't inculded in policy, i want to convert the array of days names to the dates of the current month but when i convert it is in incorrect format because it shows for different month enter image description here
You can get an array of all the dates you want the following way:
//create a array of DateTimes of the days in the month
$allDateTime = iterator_to_array(new DatePeriod(new DateTime(date('Y-m') . '-1'), new DateInterval('P1D'), new DateTime(date('Y-m-t'))));
//filter out the Fridays and Sundays
$filteredDateTimes = array_filter($allDateTime, function ($date) {
$day = $date->format("N");
return $day !== '7' && $day !== '5'; //7 for sunday, 5 for friday
});
//format the to dd-mm-yyyy
$filteredDates = array_map(function ($date) {
return $date->format("d-m-Y"); //you can choose the format you prefer here
}, $filteredDateTimes);
print_r($filteredDates);
You can run this snippet here: http://sandbox.onlinephpfunctions.com/code/8459152d9020d767110ad2732997af10d2e0d275

fetching only year and month from a date saved in the format of strtotime [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
My date string value is 1478025000. want to get year and month in the string format itself from this string,is it possible? the day should be 0 and next month and next year like that.
my desired output should be 1477852200
Try something like this:
$epoch = 1478025000;
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m'); // Display as year and month: YYYY-MM
echo $dt->format('m-Y'); // Display as month and year: MM-YYYY
If you want to get just the year and month as an epoch, try something like:
$epoch = 1478025000;
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
$year = $dt->format('Y');
$day = $dt->format('n');
$answer = new DateTime();
$answer->setDate($year, $month, 0);
$answer->setTime(0, 0, 0);
echo $answer->getTimestamp();

Comparing dates (older, newer, or the same) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Date 1: dd/mm/yy
Date 2: dd/mm/yy
Date 3: dd/mm/yy
Say I have:
$date1 = 04/07/2014;
$date2 = 04/06/2014;
$date3 = 04/07/2014;
What would be the most efficient method to determine if date 2 is between or equal to date 1 and date 3 using PHP? In other words, what is the best way to check if the dates are the same, newer, or older than another date
The best way is to convert date into timestamp, then it will be easy to compare.
Try to use
$time1 = strtotime("dd/mm/yy");
$time2 = strtotime("dd/mm/yy");
Well, this really depends on what you are using this for, what I personally did (when checking dates in a MySQL database for a scheduling website I made) I used the following:
$publishDate = $row['year'] . $row['month'] . $row['day'];
$now = date("Ynj");//four number year, two number month, two number day (YYYY/MM/DD)
You could do the same, input your date as Ynj (See PHP documentation on date here)
and then compare them. This may help put this whole idea in context; this is the code I wrote for my website:
$publishDate = $row['year'] . $row['month'] . $row['day'];
$now = date("Ynj");//four number year, two number month, two number day (YYYY/MM/DD)
if($now > $publishDate)
{
//This is saying that right now, is greater than when this was added
$msg = "This date is smaller than the time now, which means it is in the past";
}
if ($now < $publishDate)
{
echo "This date is greater than the time now, which means it is in the future.";
}
if ($now == $publishDate)
{
echo "Date is the same";
}
If you add more context to your question I am sure I can help allot more! Good luck

How to Convert M to m in date php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to convert the date like this
$original_date = date("M/d/Y"); // the output of that is Aug/27/2013
to "08/27/2013"
Like this:
date('m/d/Y', strtotime($original_date));
strtotime can convert pretty much anything reasonable you give it into a Unix timestamp, even stuff like "Next Friday".
EDIT
Funny, but seems that strtime doesn't work with a date formatted like that... The first thing that came to my mind was to replace those slashes with spaces, using str_replace or implode/explode or whatever works for you...
$newDate = date('m/d/Y', strtotime(str_replace('/', ' ', $origDate)));
$newDate = date('m/d/Y', strtotime(implode(' ', explode('/', $origDate))));
Simply by passing it back to the date function
$original_date = date("M/d/Y");
$new_date = date('m/d/Y', strtotime($original_date));
Simply change the format string:
$originalDate = date("m/d/Y"); // = 08/27/2013
Or use DateTime and specify the input format to avoid ambiguity:-
$dateString = \DateTime::createFromFormat('M/d/Y', $origanalDate)->format('m/d/Y');

Categories