I have a date in this variable
$start_date = '2021-03-04';
I want to check if this date is three months older than current date How i can check that
if($start_date < 3 months ???)
{
}
You can use Carbon to achive this:
$start_date = new Carbon\Carbon('2021-03-04');
if($start_date->diffInMonths() > 3)
{
}
You need something similar to:
$format = "y-m-d";
$start_date = \DateTime::createFromFormat($format, $start_date);
$end_date = \DateTime::createFromFormat($format, $start_date);
$end_date = $end_date->modify('+3 month');
if($start_date < $end_date)
{
}
I haven't tested the code, but you can get an idea.
You can use this :
$start_date = '2021-03-04';
if($start_date < date('Y-m-d', strtotime('-3 months'))){
}
You can use Carbon for that.
// parse your date as carbon date & only take the date string using toDateString
$start_date = Carbon::parse('2021-03-04')->toDateString();
// take the Today time, subtract 3 months from it & only take date string from it
$today = Carbon::today()->subMonths(3)->toDateString();
if ($start_date < $today) {
// whatever you wanna do here
}
Note: Carbon adds timestamp so using toDateString() you ignore that timestamp & only factor in the date string
Related
I want to get weekly dates on the basis of the start date and end date.
Suppose my start date is '2015-09-08' and end date is '2015-10-08'.
On the basis of these dates I want the following result using PHP. I want the weekly dates between the start date and end date.
2015-09-15
2015-09-22
2015-09-29
2015-10-06
you can take timestamps of both start date and end date and keep adding one weeks's timestamp to the current date until it is less than the end date timestamp.
something like below. check if this is what u asked for
$st=strtotime("2015-09-08");
$ed=strtotime("2015-10-08");
$wk=$st;
while($wk<$ed){
$wk = strtotime('+1 Week',$wk);
if($wk<$ed)
echo date("Y-m-d",$wk);
echo '<br>';
}
Try using:
select *
from table_name
where Column_name > '2015-09-08'
and Column_name < '2009-10-08'
OR
SELECT *
FROM Table_name
WHERE Column_name BETWEEN ‘2015-09-08’ AND ‘2015-10-08’
As you want weekly dates using php. The following code will do for you
<?php
$startdate='2015-09-08';
$enddate='2015-10-08';
$date=$startdate;
while($date<=$enddate)
{
$date = strtotime("+7 day", strtotime($date));
$date=date("Y-m-d", $date);
if($date<=$enddate)
echo $date."<br>";
}
?>
Php s strtotime function might come is handy here. You could try this:
$start = '2015-09-08';
$end = // you end date as string
$offset = strtotime($start);
$limit = strtotime($end);
for($t = $offset; $t < $limit; $t += 86400 * 7){
echo date('Y m d') ;
}
This link here has code to do exactly what you want, you can get the date of every Sunday or Monday or whatever day you choose between two dates
try this:
<?php
date_default_timezone_set('Asia/Kolkata');
$startdate= strtotime("15-09-08");
//$startdate= strtotime("08 September 2015");
$enddate= strtotime("15-10-08");
//$enddate= strtotime("08 October 2015");
$jump_date= $startdate;
if($enddate>$startdate)
while($jump_date< $enddate)
{
$jump_date= strtotime("+1 week", $jump_date);
if($jump_date< $enddate)
echo date('Y-m-d', $jump_date).'<br>';
}
?>
Using the built in php function strtotime to add a period of 1week
$ds='2015-09-08';
$df='2015-10-08';
$ts=strtotime( $ds );
$tf=strtotime( $df );
while( $ts <= $tf ){
$ts = strtotime('+1 week', $ts );
echo date( 'Y-m-d', $ts ).'<br />';
}
<?php
// Set timezone
//date_default_timezone_set('UTC');
// Start date
$date = '2015-09-08';
// End date
$end_date = '2015-10-08';
while (strtotime($date) <= strtotime($end_date)) {
echo $date."<br/>";
$date = date ("Y-m-d", strtotime("+7 day", strtotime($date)));
}
?>
i got 3 different Datetimes. $start_date and $end_date are the given range of dates i work with. What i need is to find the $date_from_user in this range, what is not the problem, i found plenty of solutions for that, like this SO answer.
But what i need is then to tell what exact day the $date_from_user is in the given Daterange.
So taken from the example below the expected result would be 5.
$start_date = '2009-09-05';
$end_date = '2009-09-17';
$date_from_user = '2009-09-10';
How can i get the number of the day from the range?
I thought for myself about an for loop to compare the date, but that would be problematic if the Daterange is larger than a month.
$date_from = '2014-5-25';
$date_from = strtotime($date_from);
$date_to = '2014-6-4';
$date_to = strtotime($date_to);
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$dates[] = date("Y-n-j", $i);
}
return $dates
Try the above
You should be using the DateTime classes for date calculations. They are very flexible and will take into account DST, timezones and leap years. I am at a loss as to why people are still using strtotime().
Effectively, all you need to do is count the number of days from the start of the range to the user provided date after checking that user has provided a date within the required range.
A function similar to this would work for you:-
function getDayNumberInRange($start, $end, $dayToFind)
{
$format = 'Y-m-d';
$startDate = \DateTime::createFromFormat($format, $start);
$endDate = \DateTime::createFromFormat($format, $end);
$toFind = \DateTime::createFromFormat($format, $dayToFind);
if($toFind < $startDate || $toFind > $endDate){
throw new InvalidArgumentException('The date to find must be between the start and end of the range.');
}
return $toFind->diff($startDate)->days;
}
$start_date = '2009-09-05';
$end_date = '2009-09-17';
$date_from_user = '2009-09-10';
echo getDayNumberInRange($start_date, $end_date, $date_from_user); // 5
Whats the best method to find the date in date('m.d.Y') format that is 1 day ago from another date in date('m.d.Y') format in PHP? Must be applicable to v5.1.6.
I need to build a recursive method to iterate through previous days.
Thanks!
------------Revision -------
$date = date('m.d.Y');
$date = date('m.d.Y', strtotime($date .' -1 day'));
The doesn't seem to work :(
The date must begin and end in the same format 'm.d.Y'
You really should be looking at using DateTime, DateInterval, and DatePeriod classes for this. As an example:
$start_date = '12.31.2013'; // your input date
$start_date_time = DateTime::createFromFormat('m.d.Y', $start_date);
$one_day_interval = new DateInterval('P1D');
// iterate X number of days example
$iteration_days = 30; // some value for number of iterations you want
$count_period = new DatePeriod($start_date_time, $one_day_interval, $iteration_days);
foreach($count_period as $day) {
// do something
}
// iterate between start and end date example
$end_date = '3.31.2014';
$end_date_time = DateTime::createFromFormat('m.d.Y', $end_date);
$date_period = new DatePeriod($start_date_time, $one_day_interval, $end_date_time);
foreach($date_period as $day) {
// do something
}
$date = DateTime::createFromFormat('m.d.Y', $yourDate);
$date->sub(new DateInterval('P1D');
try like this:
$date =date("Y-m-d"); //set your date
echo date('m.d.Y', strtotime($date .' -1 day'));
demo : https://eval.in/107144
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) {
..
}
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