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() )
Related
how to understand if one date in php is less than another minus one day? I mean if for example a date is set to "2018/07/03"; how can I understand if a given date is less than "2018/07/02"
date1 : year1/month1/day1
date2: year2/month2/day2
<?php
if ($year1 >= $year2) {
if ($month1 >= $month2) {
if (($day1 - 1) > $day2) {
echo 'you could do something..';
}
}
}
?>
the above code fails if forexample $year2 = 2017 and $month2 = 11.. can anybody help me? thanks a lot..
Here, this should work.
$date_to_check = new DateTime($yesterday);
$today = new DateTime();
$time_diff = $today->diff($date_to_check)->d;
if($time_diff > 1) {
echo "This is greater than one day.";
}else{
echo "This is not greater than one day.";
$date = strtotime("2018/07/01");
$date2 = strtotime("2018/07/02");
if($date > $date2){
print('date is bigger');
// do stuff when date is bigger than date2
} else {
// else ...
print('date2 is bigger');
}
To convert string to date php has function named strtotime().
Compairing date objects is simple.
There is full information about strtotime()
http://php.net/manual/ru/function.strtotime.php
Another way:
$date = new DateTime("2018/07/01");
$date2 = new DateTime("2018/07/02");
if($date->modify("+1day") > $date2){
print('date is bigger');
// do stuff when date is bigger than date2
} else {
// else ...
print('date2 is bigger or equal');
}
Notice modify modifies $date object itself.
Read more here http://php.net/manual/en/class.datetime.php
Trying to do a simple echo statement if prior to date except I'm only getting the else echo ...
<?php
if (time() < strftime('2016-01-13 00:00:00'))
{echo "You're early!";}
else {
echo "You're late!";}
?>
Little help. Thanks. Yes, I'm new to this.
Your requirement is to convert date time stamp into unix timestamp (seconds since 01-01-1970).
You can use strtotime() but, we have another accurate method for this.
Use DateTime().
<?php
$date = new DateTime('2016-01-13 00:00:00');
$timeStamp = $date->getTimestamp();
if (time() < $timeStamp) {
echo "You're early!";
}
else {
echo "You're late!";
}
?>
I have a date in the form MM-YYYY (e.g: 04-2000). I exploded the date into month and year and am now trying to check certain conditions on the month:
between 1 and 12
formed of 2 digits)
and on the year:
formed of 4 digits
Is my syntax correct?
list($name_month, $name_year) = explode('-', $name_date, 2);
if(($name_month < 1 || $name_month > 12 || $name_month ) || ($name_year)) {
echo "<br><br>Wrong date";
$uploadOk = 0;}
You can use the DateTime object and createFromFormat() to validate your date:
$date = '04-2000';
// Create a DateTime object pointing to the 1st of your given month and year
$d = DateTime::createFromFormat('d-m-Y', '01-' . $date);
if($d && $d->format('m-Y') == $date){
echo 'Valid date';
}
Eval.in
This should work, if seprator is always -
$date = '04-2000';
if($date == date('m-Y', strtotime('01-'.$date)))
{
echo 'Valid date';
}
As commented, there are several was to do this. What first comes to mind is checkdate() and createFromFormat().
The catch is, you need to be mindful what is injected for the day part since your format does not include day.
Since createFromFormat() injects the day part from the current date, it is not a viable option. George's code will fail for a format of 02-2015 on days after 28.
As such, I would use checkdate():
$date = '04-2000';
list($month, $year) = explode('-', $date);
if (checkdate($month, 1, $year)) {
echo 'Valid date';
}
<?php
date_default_timezone_set('America/Los_Angeles');
$urlYear = '2013';
$currentYear = date('Y');
$systemDate = date('d-m-Y');
if ( ($systemDate >= '01-06-'.$currentYear) || ($currentYear < $urlYear) ) {
echo 'Here are your results <br>';
echo 'System Date '.$systemDate.' '.$currentYear.' '.$urlYear;
} else {
echo 'No results for you';
}
?>
What I'm trying to achieve is:
(If the current date as formatted by me is >= '01-06'-year of the current year)
OR
(if the $currentYear < $urlYear)
//echo 'Here are your results';
But I seem to be getting true for everything. Can you pls help?
You can compare date after converting it to unix timestamp using strtotime,
if ( (strtotime($systemDate) >= strtotime('01-06-'.$currentYear)) || ($currentYear < $urlYear) ) {
echo 'Here are your results <br>';
echo 'System Date '.$systemDate.' '.$currentYear.' '.$urlYear;
} else {
echo 'No results for you';
}
DEMO.
Format your date strings YYYYmmdd when you compare, it will act like an incremental INT. With that, comparing would be easier.
$systemDate = date('Ymd');
Hope this helps.
Why don't you create DateTime objects with the dates you have and compare these
$UrlYear = new DateTime('2013' . '-01-01');
$CurrentYear = new DateTime(date('Y') . '-01-01');
$SystemDate = new DateTime();
if ( ($SystemDate >= $CurrentYear) || ($CurrentYear < $UrlYear) ) {
echo 'Here are your results <br>';
echo 'System Date '. $SystemDate->format('d/m/Y') .' '. $CurrentYear->format('Y') .' '. $UrlYear->format('Y');
} else {
echo 'No results for you';
}
compare date time using timestamp format, use mktime function to convert your date time to timestamp format
You are comparing strings lexically. This doesn't work with formatted date strings.
Transform everything to unix timestamps (UTC) and compare numbers or use date_diff functions
You can use date for comparison but it should be in Y-m-d format:
$systemDate = date( 'Y-m-d' );
$targetDate = date( 'Y-m-d', mktime( 0, 0, 0, 6, 1 ) );
if ( $systemDate >= $targetDate ) {
}
I have found hundreds of questions and answers for topics SIMILAR to this on SO, however, none match my needs specifically and I am stumped.
I have a variable in y-m-d format and I need to see if it was created on the previous calendar day (not 24 hours previous, but on the previoous calendar day).
ie.
$tDate = '12-05-2';
If object created May 2, 2012 at 11:59pm(stored time) I need a comparison to May 3, 2012 12:01 am(current time) to equal true.
If object created May 2, 2012 at 11:51pm(stored time) I need a comparison to May 2, 2012 11:58pm(current time) to equal false.
I know if these were stored in a MySQL db and pulled from a field, MySQL could figure that out easily. In this case, however, that solution is not an option.
This comparison must be done entirely in php.
I know it's an eccentric question, but hey, that's what the guru's at StackOverflow excel at! Looking forward to seeing the replies!
UPDATE
Figured this out as:
$dTest = '12-05-02';
$dTest = explode('-',$dTest);
$dTest2 = date('y-m-d');
$dTest2 = explode('-',$dTest2);
if ($dTest[2]<$dTest2[2]){
echo '<br />Posted Yesterday<br />';
} else {
echo '<br />Posted Today<br />';
}
Is there a more efficient solution? Seems to work, but I figure there must be a more optimal/elegant solution?
SOLVED
$tHolder = '12-05-12';
$voteDate = date("y-m-d", strtotime($tHolder));
$today = date("y-m-d", strtotime("today"));
if ($voteDate === $today)
{
echo "this was today's post";
}
elseif ($voteDate < $today)
{
echo "this was previous to today";
}
Firstly - I dont think your "solutions" works. What happens when todays date is 12-06-01 and the post was on 12-05-31 - it will give the wrong answer because "31" > "1"
Anyway - I think the correct answer is:
$yesterday =date("y-m-d", strtotime("yesterday"));
$today = date("y-m-d", strtotime("today"));
if ($yesterday === $tDate)
{
echo "this was yesterdays post";
}
elseif ($today === $tDate)
{
echo "this was todays post";
}
else
{
echo "this was NOT yesterday or today":
}
You can convert both dates to UNIX time and then compare it as integers:
$day_start = strtotime('-1 day', mktime(0, 0, 0);
$day_finish = strtotime('-1 day', mktime(23, 59, 59);
$dT = strtotime('-1 day', $dTime)
if($dT > $day_start && $dT < $day_finish) {
var_dump($dT);
} else {
exit;
}
If you're actually looking for "Posted X days ago":
$datetime1 = new DateTime('2012-05-01');
$datetime2 = new DateTime('2012-05-02');
$interval = (int)$datetime1->diff($datetime2)->format('%a');
switch ($interval) {
case 0:
echo "Posted Today<br />";
break;
case 1:
echo "Posted $interval day ago<br />";
break;
default:
echo "Posted $interval days ago<br />";
}