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 />";
}
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
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';
}
How would I get the nearest of two dates once I've defined them both as variables?
Here's the code to calculate the next Monday and Friday:
//calculates nearest Monday
$nextMonday = strtotime("next Monday");
$mondayParade = date("d/m/Y", $nextMonday);
//calculates nearest Friday
$nextFriday = strtotime("next Friday");
$fridayParade = date("d/m/Y", $nextFriday);
When echoing $mondayParade it displays 26/01/2015 which is correct. $fridayParade also works as above but shows friday's date.
I'd like to be able to calculate which of these two dates is closest to my current date.
I read about also using strtotime for this but can't figure out how.
Thanks
$now = time();
$nextMondayDiff = abs($now - $nextMonday);
$nextFridayDiff = abs($now - $nextFriday);
if ($nextMondayDiff < $nextFridayDiff) {
echo 'Monday is closer';
} else {
echo 'Friday is closer';
}
Or as #David points out in the comments, assuming both dates are always guaranteed to be in the future:
if ($nextMonday < $nextFriday) {
echo 'Monday is closer';
} else {
echo 'Friday is closer';
}
if($nextMonday < $nextFriday) {
echo "Monday";
} else {
echo "Friday";
}
I am trying to check if a certain time is in the past. Solution 1 works but the solutions 2 and 3 using strtotime do not. Any ideas why the strtotime solutions fail with this date when they work fine when the date is not that distant (f.ex. using 27.05.2035 works)?
<?php
$date = "27.05.2045";
$hour = "22";
$min = "15";
// 1. This one works
$datetime = DateTime::createFromFormat('d.m.Y H:i', $date.' '.$hour.':'.$min);
$now = new DateTime();
if ($datetime < $now)
{
echo "Datetime is in the past";
}
else if ($datetime > $now)
{
echo "Datetime is in the future";
}
// 2. Does not work
if (time() > strtotime($date.' '.$hour.':'.$min))
{
echo "Datetime is in the past (strtotime)";
}
else if (time() < strtotime($date.' '.$hour.':'.$min))
{
echo "Datetime is in the future (strtotime)";
}
// 3. Using another date format but still does not work
$array = explode('.', $date);
$date_converted = $array[2].'-'.$array[1].'-'.$array[0];
if (time() > strtotime($date_converted.' '.$hour.':'.$min))
{
echo "Datetime is in the past (strtotime with converted date)";
}
else if (time() < strtotime($date_converted.' '.$hour.':'.$min))
{
echo "Datetime is in the future (strtotime with converted date)";
}
?>
The 32 bit integer maximum makes it impossible to represent dates past 19 January 2038.
The solution would be to either:
Use DateTime objects, which do not represent dates using the number of seconds passed since 1970, but using a field for each time unit.
Use the 64-bit version of PHP, where the maximum integer is much higher.
See The 2038 Year Problem for more details.
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() )