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>';
}
Related
I am new to PHP and would like to compare a specific time with the current time. I have tried different tricks but nothing works for me.
Eg.
$my_time = '2:00';
$active='';
//echo "The time is " . date("h:ia");die;
if (date('h:i') > date('h:i', strtotime($myTime))) {
$active='Dinner';
$dinner=1;
}
else
{
$active='Lunch';
$lunch=1;
}
You have variable definition
$my_time = '2:00'
but you're using
strtotime($myTime))
This might help you
$my_time ="14:08:10";
if (time() >= strtotime($my_time)) {
echo "Dinner";
}else echo "Lunch";
This will compare given time with current time
try this one .
$my_time ="14:08:10";
if (time() >= strtotime($my_time))
{
echo "Dinner";
}
else
{
echo "Lunch";
}
if this doesnt work try to add date in to time.
because adding date will make time to be more specific.
I am trying to check whether user submitted date is within range or not.
<?php
$datesyntx = "/^(19|20)\d\d[\-\/.](0[1-9]|1[012])[\-\/.](0[1-9]|[12][0-9]|3[01])$/";
$paydate = "2015-03-01";
if (preg_match($datesyntx, $paydate)) {
if(strtotime($paydate) > strtotime('2015-04-23') && strtotime($paydate) < strtotime('2015-07-23')) {
}
}
?>
It is not working what actually I am trying to get.
Try this code :
$paydate = date('2015-03-01');
$DateBegin = date('2010-01-01');
$DateEnd = date('2016-01-01');
if (($paydate > $DateBegin) && ($paydate < $DateEnd))
{
echo "is between";
}
else
{
echo "not between!";
}
P.S: this question was answered before at stackoverflow:
PHP check if date between two dates
EDIT2: easy way .
No need to use regex in this scenario, use simple DateTime class to check whether user submitted date is within range or not.you can also use typical strtotime() to do this job. But DateTime class will be great fun for this.
<?php
$userSubmitted = new DateTime("2015-04-01");
$startDate=new DateTime("2014-02-01 20:20:00");
$endDate=new DateTime("2015-04-30 23:50:11");
if ($userSubmitted > $startDate && $userSubmitted < $endDate)
{
return true; #date is within the range of yours
}
return false; #date not within the range of yours
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";
}
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';
I have a date like this: 16/02/2011
What I want to do is have a simple PHP conditional that checks if todays date is either that DAY or AFTER so for example:
<?php
$mydate = '26/01/2010';
if($mydate == date('dd/mm/yyyy')
{
echo 'last day to reply';
}
elseif($mydate == 'date after todays date')
{
echo 'post has expired and you cannot reply';
}
else
{
echo 'post has NOT expired and you can reply';
}
?>
So if today's date is 01/01/2011 then it would say post has not expired
if date is 17/02/2011 then it would say last day to reply
and if the date is after 25/02/2011 then it would say it has expired.
Can anyone help? Thanks :)
The easiest way would be to use mktime to convert the required date and times (the last date and the ultimate deadline) and then directly compare the current time against the those.
the easiest way is to have a date already in the proper format. Especially if it's coming from database.
<?php
$mydate = '2010-01-26';
$curdate = date('Y-m-d');
if($curdate == $mydate)
{
echo 'last day to reply';
}
elseif($curdate > $mydate)
{
echo 'post has expired and you cannot reply';
}
else
{
echo 'post has NOT expired and you can reply';
}
?>
note that by reading this code you will have no problem understanding what does it do.
it's almost natural language and self-explanatory
unlike all other codes here.
Rather than compare visual dates it's probably better to compare timestamps:
$deadline = strtotime('2010-01-26');
$today = mktime(0, 0, 0);
if ($today == $deadline) {
echo 'last day';
}
else if ($today > $deadline) {
echo 'past day';
}
else {
echo 'a-okay';
}
In simplistic terms, you'd want to do somethinn like:
if (strtotime($date) > date()) {
echo "Your post has expired";
}
it's very difficult to compare dates-that-are-strings, especially when you consider that "2/3/4" could be any of 8 different dates (Feb 3rd, '04; Mar 4th, '02; etc..). Keeping dates/times as actual timestamp values in PHP makes the comparisons far easier.
You can use mktime (or strotime if you can get your end date in a proper format, 17-02-2011 or 02/17/2011):
$mydate = '17/02/2011';
$mydate_parts = explode('/', $mydate);
$mydate_timestamp = mktime(0, 0, 0, $mydate_parts[1], $mydate_parts[0], $mydate_parts[2]);
if($mydate == date('d/m/Y'))
{
echo 'last day to reply';
}
elseif($mydate_timestamp < time())
{
echo 'post has expired and you cannot reply';
}
else
{
echo 'post has NOT expired and you can reply';
}
I would strongly recommend you look up on the DateTime class documentation; if you're not interested though; a string slice would work.
Something along the lines of the following is probably the quickest for you.
if( mktime(0,0,0,substr($mydate, 3, 2), substr($mydate, 0, 2), substr($mydate, 6, 4) ) > time() )