In the following code $start is a start date manually entered into a datepicker and $end is a separate key also, entered via datepicker. These are being compared against date('ymd'), which is today.
Early in the code for this plugin we have this code (where the same argument returns true):
//Parse End Date
if($end):
$end = explode('-', $end);
$end = mktime($hour, $_POST['_date_minute'], 0, $end[0], $end[1], $end[2]);
if ((date('ymd',$start) < date('ymd',$end)) && (date('ymd',$end) >= date('ymd'))) {
$compare = date('ymd'); //Overwrite start date $compare
}
else {
$compare = date('ymd', $start);
}
endif;
Later in the code, the same argument returns false here:
function event_list_date($start_or_end, $format, $echo = true){
global $post;
// Check the end date, if it's greater than today and then start date is less than or equal to today, round it off so that it's today and doesn't look like the event is already past
// Original Code
// if ((date('ymd',$start) < date('ymd',$end)) && (date('ymd',$end) >= time('ymd'))) {
// Stackoverflow Proposed Change
// if ($start < $end && $end >= time('ymd')) {
if ($start < $end && $end >= time('ymd')) {
$start = date('ymd'); //Overwrite start date $compare
}
else {
$start = get_post_meta($post->ID, '_date_start', true);;
}
$end = get_post_meta($post->ID, '_date_end', true);
if($start_or_end == 'start'):
$date = date($format, $start);
if($echo): echo $date; else: return $date; endif;
elseif($start_or_end == 'end'):
$date = date($format, $end);
if($echo): echo $date; else: return $date; endif;
endif;
}
Can someone tell me why the if statement is returning false for an event with a $start value equal to yesterday and an $end equal to 5 days from now?
EDIT: Posting a "zoomed out" context of this code
The date function changes these into strings. Then you're using mathematical comparison operators to compare the strings, which does not work in the way you're wanting. If you want to compare a date against another date, you'll want to convert them to something more easily compared, like numbers.
strtotime will take care of that! Since you're passing $start into date() (which requires a numeric argument), I assume $start is already a numeric time representation. So, to compare against right now, use time();
strtotime docs
date docs
Related
This question already has answers here:
How to check if a date is in a given range?
(10 answers)
Closed 4 years ago.
I get two values from GET
$start = $_GET['start'];
$end = $_GET['end'];
These are:
1-11-2018
30-11-2018
Then I remove the dash to create a whole number
$start = str_replace(["-", "–"], '', $start);
$end = str_replace(["-", "–"], '', $end);
Now we have:
1112018
30112018
Then we do a loop over our posts (we only have 2 posts) and we grab the value from a custom field:
$myDate = get_post_meta($id, 'usp-custom-80', TRUE);
Which gives us:
13-11-2017
26-11-2018
And then we do:
$myDate = str_replace(["-", "–"], '', $start);
And we have:
13112017
26112018
So now we can check if the value we're getting from the custom field is within or not the values we have from GET
if (($myDate >= $start) && ($myDate <= $end)) {
//content....
But I am getting the logic wrong, also because the dates from GET could have 1 without a zero at the beginning 01 and the number would be less even tho the actual date is correct to be considered within it.
Any idea how I can check if $myDate is in between $start and $end?
UPDATE
If I don't remove dash and I get:
Start date: 1-11-2018
End date: 30-11-2018
User date: 13-11-2017
And then I simply do:
if ( ( $myDate >= $start) && ( $myDate <= $end) ) {
I get ALL posts and not the filtered by range
You can simply do this way with DateTime class,
<?php
$myDate = new DateTime('26-11-2018'); // 13-11-2017 is not between you can test
$start = new DateTime('1-11-2018');
$end = new DateTime('30-11-2018');
if ($myDate > $start && $myDate < $end ){
echo "Date is between";
}else{
echo "Date is not between!";
}
?>
DEMO: https://3v4l.org/Grv1X
You can use strtotime() to parse date then compare dates like below-
$start = strtotime('1-11-2018');
$end = strtotime('30-11-2018');
$myDate = strtotime('13-11-2017');
if ( ( $myDate >= $start) && ( $myDate <= $end) ) {
......
// your other code goes here
}
I have this time range in array example:
$timerange = array('01:30:00','01:31:00',...........,'02:30:00');
and 2 variable:
$start_time = '01:15:00';
$end_time = '03:29:00';
if($timerange is between $start_time && $end_time)
{
//do it something if yes.....
}
Please help me, its have any ready function to use in PHP? to check on this.
You need not bother with conversions of your time strings to a time type - you can compare the strings as they are:
<?php
$timerange = array('01:30:00', '01:31:00', '01:32:00', '02:30:00');
$start_time = '01:15:00';
$end_time = '03:29:00';
$between = array();
foreach ($timerange as $time)
if ($start_time <= $time && $time <= $end_time) $between[] = $time;
if ($between)
{
echo "These are the times between $start_time and $end_time:\n";
print_r($between);
}
If you like it better, you can replace the foreach loop with array_filter():
$between = array_filter($timerange,
function($time) use ($start_time, $end_time)
{ return $start_time <= $time && $time <= $end_time; }
);
<?php
$timerange = array(strtotime('01:30:00'), strtotime('01:31:00'), strtotime('03:30:00'));
$start_time = strtotime('01:15:00');
$end_time = strtotime('03:29:00');
foreach($timerange as $key => $text_field){
if($timerange[$key] > $start_time && $timerange[$key] < $end_time){
echo "Existing";
}else{
echo "Not Existing";
}
}
?>
See How to check if a date is in a given range?
Edit: As you are looking in a 24 hour range you can pick and random date when constructing your timestamps and your calculations should hold true as all of them are the same date.
return (($user_ts >= $start_ts) && ($user_ts <= $end_ts));
Where all of those are timestamps.
Also look at this PHP check if time falls within range, questioning common solution if you don't want this to depend on the date but just the time.
We keep getting the above error. Have tried adding a $start above 171 but same result. Here is the code :
/**
* Get all of the months since a certain date
*/
public static function getMonthsSinceDate($start) {
$key_month = date('MY', $start); (note this is Line 171)
$key = 'months_since_' . $key_month;
$months = CodonCache::read($key);
if ($months === false) {
if (!is_numeric($start)) {
$start = strtotime($start);
}
$end = date('Ym');
do {
# Get the months
$month = date('M Y', $start);
$months[$month] = $start; # Set the timestamp
$start = strtotime('+1 month +1 day', strtotime($month));
# Convert to YYYYMM to compare
$check = intval(date('Ym', $start));
} while ($check <= $end);
CodonCache::write($key, $months, 'long');
}
return $months;
}
/**
Wherever you are calling the function getMonthsSinceDate(); you are accidentally passing it something non numeric. It could be subtle like a number wrapped in quotes which would become a string, i.e
getMonthsSinceDate('111111')
PHP's date() expects to be given a date in an integer UNIX timestamp. If you are trying to pass a human-readable date as a string it will throw an error.
If you are, convert it to a timestamp using strtotime(), like this:
$start = strtotime( $start );
Just wanted to double check and make sure my line of thinking is correct. This hook runs every 48 hours, so I need to check if an event is happening today or tomorrow.
$now = date('Y/m/d');
$today = explode("/", $now);
The event start date has the same format, but the value varies, and is stored the same way.
if ( $today[1] == $eventDate[1] &&
(intval($today[2]) == (intval($eventDate[2]-1)) || (intval($today[2]) == intval($eventDate[2])-2))) {
//run code
}
In my opinion, you should consider to work with DateTime objects :
$today = new \DateTime();
$eventDateTime = \DateTime::createFromFormat('Y/m/d', $eventDate);
if ($today->format('d-m-Y') === $eventDateTime->format('d-m-Y')) {
...
}
If you want to check that an event is happening today or tomorrow, you could do like this :
$start = new \DateTime();
$start->setTime(0,0,0);
$end = new \DateTime();
$end->add(new \DateInterval('P1D'));
$end->setTime(23,59,59);
$eventDateTime = \DateTime::createFromFormat('Y/m/d', $eventDate);
if ($eventDateTime >= $start && $eventDateTime <= $end) {
...
}
This way you check if a date is in a certain period. Here I added 1 day to the current date but you can adapt the code and set more than 1 day if you want.
I am new in php.
I have 2 dates that I am getting from 2 drupal fields and each field is saved in database with it´s own date format so the formats:
$old_format_starts -> has this date format: date('Y-m-d H:i:s'); and
$old_format_ends -> has this date format: date('jS F Y H:i:s');
So I am using the following code:
$old_format_starts = $entity->field_video_available_d[LANGUAGE_NONE][0]['value'];
$old_format_ends = $entity->field_video_until[LANGUAGE_NONE][0]['value'];
After that, I am converting those date information to this date format: date('Y-m-d'); like this:
$starts = date("Y-m-d", strtotime($old_format_starts));
$ends = date('Y-m-d', strtotime($old_format_ends));
In order later I can use that information for comparing if today is between the dates $starts and &ends, and if today is between those dates then I will print something.
So here is the total code that I am using, but I have problems with the date formats... something I am doing wrong because the page is just taking long time in load the page and in the end it does not load.... The problem is coming when I am using the WHILE bucle, because I tried just using the code before the WHILE and the page loads in a normal way. So I dont know If I am using wrong the WHILE BUCLE or the strtotime function or dates format...?
<?php
$old_format_starts = $entity->field_video_available_d[LANGUAGE_NONE][0]['value'];
$old_format_ends = $entity->field_video_until[LANGUAGE_NONE][0]['value'];
$starts = date("Y-m-d", strtotime($old_format_starts));
$ends = date('Y-m-d', strtotime($old_format_ends));
$to= date("Y-m-d", strtotime($to));
$i = date("Y-m-d", strtotime($starts));
while ($i <= $ends) {
if ($i==$to) {
print date('Y-m-d', strtotime($i));
break;
}
$i=date("Y-m-d", strtotime('+1 day', $i));
}
?>
I really have tried already many ways.. but without success.... here is the same code but using the FOR bucle... and also without success..
<?php
$old_format_starts = $entity->field_video_available_d[LANGUAGE_NONE][0]['value'];
$old_format_ends = $entity->field_video_until[LANGUAGE_NONE][0]['value'];
$starts = date("Y-m-d", strtotime($old_format_starts));
$ends = date('Y-m-d', strtotime($old_format_ends));
$to= date("Y-m-d");
for ($i = $starts; $i <= $ends; $i++) {
if ($i==$to) {
print $i;
break;
}
}
?>
PLEASE HELP!!!! Any sugestion is welcome!!! There has to be a solution... Something I am doing wrong in the bucles (While) or (FOR) because the page is not loading!!!
Thank you!!! :)
If you compared the strtotime() values directly, it would work. strtotime() returns simple integers and >/</= type comparisons will work as expected. In your code, you're converting them to strings using date(), in a least-significant-value-first ordering, so your comparisons won't work as expected.
Comparing the dates using strtotime():
$start = strtotime($old_format_starts);
$end = strtotime($old_format_ends);
$today = time();
if ($today > $start && $today < $end) {
// code
}
Comparing the dates using the DateTime class:
$start = new DateTime($old_format_starts);
$end = new DateTime($old_format_ends);
if ($today > $start && $today < $end) {
// code
}
Once the comparison is over, you can format the date strings using date() function (if you're using strtotime()) or the format() method (if you're using DateTime class).
The while loop is not necessary here
I suggest the use of the DateTime native class like this :
$old_format_starts = $entity->field_video_available_d[LANGUAGE_NONE][0]['value'];
$old_format_ends = $entity->field_video_until[LANGUAGE_NONE][0]['value'];
$starts = DateTime::createFromFormat('Y-m-d H:i:s', $old_format_starts);
$ends = DateTime::createFromFormat('jS F Y H:i:s', $old_format_ends);
$today = new DateTime() ;
if(($today > $start) && ($today < $end)){
//Do your thing
}