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
}
Related
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
How to calculate if something has expired based on date and time in php backend. Also it should work for users regardless of time zone.
Timestamp in database
2020-07-24 21:24:27
Timeout value comes from api
00:03:47
if((2020-07-24 21:24:27 + 00:03:47) < date()) {
echo "expired";
} else {
echo "pending";
}
You dont have timezone info in your dates so i assume dates from db are in the same timezone as server.
Use DateTime, its add() method to add the time from api to it as a DateInterval and then compare it with current time to get the result.
<?php
$timestamp = '2020-07-24 21:24:27';
$timeout = '00:03:47';
function isExpired(string $timestamp, string $timeout): bool {
$timestampDate = new DateTime($timestamp);
$timeoutParts = explode(':', $timeout);
$timestampDate->add(new DateInterval(sprintf('PT%sH%sM%sS', $timeoutParts[0], $timeoutParts[1], $timeoutParts[2])));
return $timestampDate < new DateTime();
}
// now for me is 2020-07-24 20:37:00
var_dump(isExpired($timestamp, $timeout)); // false
var_dump(isExpired('2020-07-24 20:00:00', '01:00:00')); // false
var_dump(isExpired('2020-07-24 19:00:00', '01:00:00')); // true
var_dump(isExpired('2020-07-23 19:00:00', '12:00:00')); // true
var_dump(isExpired('2020-07-25 19:00:00', '12:00:00')); // false
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
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.
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 7 years ago.
Improve this question
I have a date format of DD/MM/YYYY and I want to check in a form submission whether the submitted date is present in the calendar or not.
Code:
$last_date = $_POST['last_date'];
$date = str_replace("/", "-", $last_date);
$d = explode("-" , $date);
if( !checkdate($d[1], $d[0], $d[2]) )
error[] = 'Warning! This date does not have any existence.';
To validate a Gregorian date you should use PHP's checkdate() function
$date_to_check = checkdate('2', '29', 1969) ? 'good' : 'bad';
echo $date_to_check; // 'bad'
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
How can I calculate the mid-point between two dates in PHP
In Java Script we do -
var midpoint = new Date((date1.getTime() + date2.getTime()) / 2);
Any help would be appreciated.
Try this:
$midPoint = (strtotime($date1) + strtotime($date2))/2;
This is done fairly similarly to javascript actually try this
$midpoint = ($date1 + $date2) / 2
This assuming $date1 and $date2 are timestamps ( the same that is generated from getTime() )
I have found a way for this -
$daysDiff = diffDateTime($StartDate, $EndDate);
$midDaysDiff = round($daysDiff['days']/2);
$midDate = strtotime(date("m/d/Y", strtotime($StartDate)) . "+ $midDaysDiff Days");
function diffDateTime($StartDate, $EndDate)
{
// returns diff between two dates in days...
}