PHP How to find if 2 dates are equal - php

I have a date field that submits the chosen dates and I want to find out if the end date is today.
So for example I want to compare 21-May-2012(today) to 21-May-2012(submitted) and see if they match or not.
Can someone please help?
Thanks in advance

if ( strtotime($current) != strtotime($sumbit) )
Or just simplier:
$currentDate = '21-May-2012';
$submit = '21-May-2012';
if ($submit != $currentDate)
echo 'Different';

$currentDate = '21-May-2012';
$submitDate = '21-May-2012';
if ($submitDate == $currentDate)
echo 'Same';
else
echo 'Different';

Related

check whether a date falls in between date range concat with TO operator

I have following format 2016-06-06 TO 2016-06-12
I want to know that whether my date i.e 2016-06-11 lies in between or not. How can I do this?
You can use PHP if condition for checking date range:
$compareDate = "2016-06-06 TO 2016-06-12";
$dateArr = explode(" TO ",$compareDate);
$starting_date = $dateArr[0];
$final_date = $dateArr[1];
$date = "2016-06-11";
if($date >= $starting_date && $date <= $final_date) {
...
}
if($date >= $fome_date && $date <= $to_date)
{
echo "yes";
}
else
{
echo "no";
}
https://3v4l.org/UdIgq
i hope it will be helpful
Dates can be compared just as numbers can be
So using
date > starting_date && date < final_date
Will be just fine for an if clause. Also if you have I would recommend you do this in the database part as dbs have built in queries for such occasions.

comparing date not Working in php

I have two dates here and want to compare with another date ...
another date here is called $date_main and compared it with a variable called $date_to ... Help it's not working..
<?php
$date_f="02-05-2014";
$date_t="02-11-2014";
$date_from=date('d-m-Y', strtotime($date_f));
$date_to=date('d-m-Y', strtotime($date_t));
$date_c="21-7-2014";
$date_main=date('d-m-Y', strtotime($date_c));
if(($date_main<$date_to))
{
echo "date_main is less then to<br/>";
}
?>
You'll have to compare the times and not the dates itself:
if(strtotime($date_c) < strtotime($date_t))
I'm wondering why not doing something like this?
$date_f = "02-05-2014";
$date_t = "02-11-2014";
$date_from = DateTime::createFromFormat('d-m-Y', $date_f);
$date_to = DateTime::createFromFormat('d-m-Y', $date_t);
$date_main=DateTime::createFromFormat('d-m-Y', '21-07-2014');
if (($date_main < $date_to)) {
echo "date_main is less then to<br/>";
}
Working with objects is always better.
$date_f="02-05-2014";
$date_t="02-11-2014";
$date_main=date_create("2014-7-21");
$date_to=date_create("2014-11-02");
$diff=date_diff($date_main,$date_to);
if($diff->format("%R%a")>0)
{
echo "date_to is greater";
}
else
{
echo "date_main is greater";
}

Only show html on certain days w/php

I only want to show some html on certain days of the week. I think I am very close, but cannot get this to work. Thanks for the help.
<?php
$dayofweek = date('l');
$daystoshow = array('Thursday','Sunday','Wednesday');
if ($dayofweek == $daystoshow) {
echo "show this";
}
?>
Use in_array() to check to see if a value in in an array:
if (in_array($dayofweek, $daystoshow)) {
Allright:
Lets use PHP date function
Do you want to show it only during a particular month ?
$date = date("m");
if ($date == "01") { // only on january
//output
}
Do you want to show it only during a particular day ?
$date = date("d");
if ($date == "01") { //Only on the first of the month
//output
}
Do you want to show it only during particular dayS ?
$days = array("01","11","28");
$date = date("d");
if (in_array($date,$days)) { //Only 01,11,28
//output
}
Do you want to show it only during particular days of week ?
$days = array("Thursday","Sunday","Wednesday");;
$date = date("l");
if (in_array($date,$days)) {
//output
}
Do you want to show it only during a particular year ?
$date = date("Y");
if ($date == "2014") { //Only on 2014
//output
}

Echoing something out on a specific date every year

So I'm trying to make a message appear on one specific date of the year.
My code now:
<?php
$year = date("Y");
if(checkdate(5, 22, $year) === TRUE) {
echo '<b>something</b>';
}else {
echo '';
}
?>
But the message appears howsoever, no matter the date.
Hope you can help me,
Thanks in advance!
If you don't have to use checkdate, this is an alternative.
<?php
if (date('Y-m-d') == date('Y').'-05-22') {
echo '<b>something</b>';
} else {
echo '';
}
?>
According to the documentation checkdate only validates dates, it doesn't compare them against the current date.
Returns TRUE if the date given is valid; otherwise returns FALSE.
Use something similar to the code you already have to check the date
if( date("n j") == "5 22")
{
echo '<b>something</b>';
}

If/then statement when dealing with a date

I'm new to PHP and could use a little help figuring this out. I have a drupal website where I have a field that is called 'field_date'. Inside that field there are two variables (date1 & date2) which are the start and end dates. I am trying to create a statement that says:
If the current date is between date1 and date2 then display 'x'. If it is out of that date range then display 'y'.
I can also get the current date by using the drupal date variable:
format_date(time(), 'custom', 'F d Y');
Thanks for anyone able to assist. I really do appreciate it!
UPDATE: For anyone that has this same question I got some help here.
convert your variables into one form and put them into if-statement
$now = time();
$date1 = strtotime($date1);
$date2 = strtotime($date2);
if ( $now >= $date1 && $now <= $date2) {
echo 'x';
} else {
echo 'y';
}
here we go
$current_day = 'some day here';
$start_day = strtotime('20-Feb-2012'); // or how u have the date
$end_day = strtotime('22-Feb-2012'); // or how u have the date
if ($current_day >=$start_day && $current_day<=$end_day)
{
echo 'x';
}
else
{
echo 'y';
}

Categories