how to check if a date is three days before today - php

Hey i would like to know if there is any script (php) that could check if a specified date three days before today.
say..
$d1 = date("Y-m-d", filemtime($testfile));
$d2 = date("Y-m-d");
now i would like to know how to compare this two dates to check if d1 is atleast 3days ago or before d2
any help would be gladly appreciated.

Why not to use DateTime object.
$d1 = new DateTime(date('Y-m-d',filemtime($testfile));
$d2 = new DateTime(date('Y-m-d'));
$interval = $d1->diff($d2);
$diff = $interval->format('%a');
if($diff>3){
}
else {
}

Assuming you wish to test whether the file was modified more than three days ago:
if (filemtime($testfile) < strtotime('-3 days')) {
// file modification time is more than three days ago
}

Just check it with timestamp:
if (time() - filemtime($testfile) >= 3 * 86400) {
// ...
}

use date("Y-m-d", strtotime("-3 day")); for specific date
you can also use
strtotime(date("Y-m-d", strtotime("-3 day")));
to convert it to integer before comparing a date string

well, stunned to see no one is using mktime() function,
it makes the job simple
for example your input date is :10/10/2012
mktime convert it to unix time stamp
$check_date=mktime(0,0,0,10,**10+3**,2012);
we can perform any operations weather +,-,*,/

use timestamp instead of date,
$d1 = filemtime($testfile);
$now = time();
if ($now - $d1 > 3600*24*3) {
..
}

Related

php add x weeks to a date and then find the next given day

I can add x week to my date
//$ultima_azione <--- 2015/07/15
//$data['intervallo'] <---- 5
$mydate = date("Y-m-d",strtotime($ultima_azione." +".$data['intervallo']." weeks"));
now how can i give a day starting from that week
example:
//$mydate + "next Monday" -----> final date
and this ve to work like, if today is Monday and i add weeks to jump to an other Monday and then i select the next Monday the week don't ve to change
The simplest way would be to use strtotime. It can do date calculations based on a textual representation of the delta:
$mydate = strtotime('+3 weeks');
It also accepts a second parameter, which is a timestamp to start from when doing the calculation, so after you get the offset in weeks, you can pass the new date to a second calculation:
// Get three weeks from 'now' (no explicit time given)
$mydate = strtotime('+3 weeks');
// Get the Monday after that.
$mydate = strtotime('next Monday', $mydate);
See strtotime documentation for more examples of notations that you can use.
I would highly recommend using PHP's built-in DateTime class for any date and time logic. It's a much better API than the older date and time functions and creates much cleaner and easier to read code.
For example:
// Current date and number of weeks to add
$date = '2015/07/15';
$weeks = 3;
// Create and modify the date.
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('next monday');
// Output the new date.
echo $dateTime->format('Y-m-d');
References:
DateTime.
DateTime::createFromFormat
DateTime::add
DateTime::modify
DateInterval::createFromDateString
DateTime::format
Are you looking for something like this?
$today = time();
$weeks = 2;
// timestamp 2 weeks from now
$futureWeeks = strtotime("+ ".$weeks." weeks");
// the next monday after the timestamp date
$futureMonday = strtotime("next monday",$futureWeeks);
echo date("Y-m-d", $futureMonday);
// or in one line
echo date("Y-m-d", strtotime("next monday", strtotime("+ ".$weeks." weeks")));
PHP is using an unix timestamp for date calculations. Functions as date() and strtotime() using a timestamp as an optional second parameter. This is used a reference for formatting and calculations. If no timestamp is passed to the function the current timestamp is used (time()).
I have the answer here. This will show the next wednesday every 2 weeks and the first date to start from would be the 10th.
I have also added in an estimated delivery which would be 6 weeks after that date.
We will be placing our next order for this on:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('wednesday');
echo $dateTime->format('d/m/Y');
?>
Expected delivery for the next order will be:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('+42 days next wednesday');
echo $dateTime->format('d/m/Y');
?>
If anyone can confirm this is correct that would be great.

Calculating MySQL and PHP date difference in days

I am trying to calculate a date difference in days using MySQL and PHP date.
My code
$ArrivalDate = $variants_data['ArrivalDate'];
$daydiff=floor((abs(strtotime(date("d/m/Y")) - strtotime($ArrivalDate))/(60*60*24)));
Output
<td>'.$daydiff.'</td>
Results
I get 93 days instead of 26 days (got 26 days using this calculator http://easycalculation.com/date-day/number-of-days.php)
ArrivalDate value = 2013-05-03 from MySQL table and it changes due to transport delays, etc.
How can I achieve this in PHP?
try this
$daydiff=floor((abs(strtotime(date("Y-m-d")) - strtotime($ArrivalDate))/(60*60*24)));
just change your current date function format so it will give your correct answer means 26 days.
Use DateTime class :
$today = new DateTime;
$oneWeekLater = clone $today;
$oneWeekLater->modify('+1 week');
$diff = $today->diff($oneWeekLater);
echo $diff->format('Y-m-d H:i:s');
http://www.php.net/manual/en/class.datetime.php
http://www.php.net/manual/en/class.dateinterval.php
$diff = strtotime(date("d/m/Y")) - strtotime($ArrivalDate);
echo "Difference is $diff seconds\n";
$days = floor($diff/(3600*24));
echo "Difference is $days days\n";
You can also do it at the database level using the DATEDIFF() function.
http://www.w3schools.com/SQl/func_datediff_mysql.asp
$days = date("d", $timestamp1) - date("d", $timestamp2);
Try this
$ArrivalDate = '2013-05-03';
echo $daydiff=floor((abs(strtotime(date("Y-m-d")) - strtotime($ArrivalDate))/(60*60*24)));
Your date function was not in proper format as that of arrival date

Adding to datetime in php

If i have:
if($date>=$start_interval...){
//Some code
}
Where both $date (current date) and $start_interval are both in datetime format - like YYYY-MM-DD 00:00:00. What do i need to do to add an hour (or 60 minutes) to $start_interval?
So the if statement would be - if($date>=$start_interval (+hour)) - in other words if its more than an hour later do something. How would you do this?
Many thanks in advance
Look into DateTime. It makes working with dates easy.
$now = new DateTime();
$start_interval = new DateTime('2012-12-14 00:00:00');
$start_interval->modify('+1 hour');
if ($now >= $start_interval)
{
// do something
}
<?php
if (strtotime($date) >= strtotime('+1 hour', strtotime($start_interval)) {
//Some code
}
More info: http://es1.php.net/manual/en/function.strtotime.php

how to find number of days by subtracting previous date from current date

Is it possible to find the no of days between two date fields.
i want to remove the user if user does not login within 30 days.
on every login login_date field will update.
i want to subtract two fields login_date and current_date
and if answer is 30 or greater than 30 it will delete that user.
i am new in php and need help..
i am working on localhost.
I suggest to use DateTime and DateInterval objects.
$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "days difference ".$interval->d." days ";
read more php DateTime::diff manual
If you use timestamp format - you can use condition:
$month = 30*86400;
if ($current_date - $login_date > $month){
delete_user();
}
If you use datetime format - you can transform this format to timestamp with function strtotime
Hi try this function will get number of days between two dates.
function dateDiff($start, $end) {
$start_ts = strtotime($start);
$end_ts = strtotime($end);
$difference = $end_ts - $start_ts;
return round($difference / 86400);
}
thanks
Following will return exact days between two dates.
$last_login_date = "2012-04-01";
$current_date = "2012-04-30";
echo "Days: ".round(abs(strtotime($current_date)-strtotime($last_login_date))/86400);
Hope this helps.

Figuring out that today is x number of days is after given date in PHP

I have data in my database that returns the month/day/year data points for events.
What I want to do is check whether today is 20 days after the month/day/year that I get.
So far I have something like:
// Get date data
$day = $row['DAYOFMONTH(hike_date)'];
$year = $row['YEAR(hike_date)'];
$month = $row['MONTH(hike_date)'];
// Get today's date
$todays_date = date("Y-m-d");
// Create the date I am comparing with
$date_string = $year.'-'.$month.'-'.$day;
My question is how do I compare it? And is the format going to matter in comparing the two dates?
Thanks!
Parse the month/day/year into a DateTime:
$other = date_create($year.'-'.$month.'-'.$day);
Add 20 days to it:
$twenty_days_after = clone $other;
$twenty_days_after->modify('+20 days');
Compare it with today:
if ($today >= $twenty_days_after) {
// today is 20 days after the month/day/year that you got
}
See date_create.
$database_date = strtotime(sprintf('%s-%s-%s', $year,$month,$day));
$twenty_days_from_now = strtotime('+20 days');
$twenty_days_From_database = strtotime(sprintf('%s-%s-%s +20 days', $year,$month,$day));
Using strttime is the easiest method.
use
strtotime()
function which will convert your dates into timestamp and you can compare them easier
// Get date data
$day = $row['DAYOFMONTH(hike_date)'];
$year = $row['YEAR(hike_date)'];
$month = $row['MONTH(hike_date)'];
// Get today's date
$todays_date = date("Y-m-d");
// Create the date I am comparing with
$date_string = $year.'-'.$month.'-'.$day;
if ( date( 'Y-m-d', strtotime( "+20 days", $date_string ) ) {
//date is 20 days before today
}
strtotime does magical things

Categories