I use
$date = date("Y-m-d");
and in sql i use between max is = date("Y-m-d") min is = 6 days back
Is there a function that gives from date back $limit?
In PHP, it could be done like this:
$date_first = date("Y-m-d"); //today's date or use some other date
$date_second = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date_first)) . " -6 day")); //date before 6 days
EDIT
Based on Dan Lee's suggestion(see the comment below):
$date_before = date("Y-m-d", strtotime("-6 day"));
You can use MySQL only for this task.
Take DATE_SUB() to subtract from the current time:
SELECT * FROM table WHERE date BETWEEN DATE_SUB(NOW(), INTERVAL 6 DAY) AND CURDATE()
You could also use the OO variation of #AkhileshBChandran's answer:
$dt = new DateTime('-6 days');
$sixDaysAgo = $dt->format('Y-m-d');
Related
I'm trying to get a date that is one year from the date I specify.
My code looks like this:
$futureDate=date('Y-m-d', strtotime('+one year', $startDate));
It's returning the wrong date. Any ideas why?
$futureDate=date('Y-m-d', strtotime('+1 year'));
$futureDate is one year from now!
$futureDate=date('Y-m-d', strtotime('+1 year', strtotime($startDate)) );
$futureDate is one year from $startDate!
To add one year to todays date use the following:
$oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", mktime()) . " + 365 day"));
For the other examples you must initialize $StartingDate with a timestamp value
for example:
$StartingDate = mktime(); // todays date as a timestamp
Try this
$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($StaringDate)) . " + 365 day"));
or
$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($StaringDate)) . " + 1 year"));
//1 year from today's date
echo date('d-m-Y', strtotime('+1 year'));
//1 year from from specific date
echo date('22-09-Y', strtotime('+1 year'));
hope this simpler bit of code helps someone in future :)
Try: $futureDate=date('Y-m-d',strtotime('+1 year',$startDate));
just had the same problem, however this was the simplest solution:
<?php (date('Y')+1).date('-m-d'); ?>
// Declare a variable for this year
$this_year = date("Y");
// Add 1 to the variable
$next_year = $this_year + 1;
$year_after = $this_year + 2;
// Check your code
echo "This year is ";
echo $this_year;
echo "<br />";
echo "Next year is ";
echo $next_year;
echo "<br />";
echo "The year after that is ";
echo $year_after;
I prefer the OO approach:
$date = new \DateTimeImmutable('today'); //'today' gives midnight, leave blank for current time.
$futureDate = $date->add(\DateInterval::createFromDateString('+1 Year'))
Use DateTimeImmutable otherwise you will modify the original date too!
more on DateTimeImmutable: http://php.net/manual/en/class.datetimeimmutable.php
If you just want from todays date then you can always do:
new \DateTimeImmutable('-1 Month');
If you are using PHP 5.3, it is because you need to set the default time zone:
date_default_timezone_set()
strtotime() is returning bool(false), because it can't parse the string '+one year' (it doesn't understand "one"). false is then being implicitly cast to the integer timestamp 0. It's a good idea to verify strtotime()'s output isn't bool(false) before you go shoving it in other functions.
From the docs:
Return Values
Returns a timestamp on success, FALSE
otherwise. Previous to PHP 5.1.0, this
function would return -1 on failure.
Try This
$nextyear = date("M d,Y",mktime(0, 0, 0, date("m",strtotime($startDate)), date("d",strtotime($startDate)), date("Y",strtotime($startDate))+1));
There is also a simpler and less sophisticated solution:
$monthDay = date('m/d');
$year = date('Y')+1;
$oneYearFuture = "".$monthDay."/".$year."";
echo"The date one year in the future is: ".$oneYearFuture."";
You can use strtotime() to get future time.
//strtotime('+1 day');
//strtotime('+1 week');
//strtotime('+1 month');
$now = date('Y-m-d');
$oneYearLaterFromNow = date('Y-m-d', strtotime('+1 year'));
$oneYearLaterFromAnyDate = date('Y-m-d', strtotime('+1 year', strtotime($anyValidDateString)));
In my case (i want to add 3 years to current date) the solution was:
$future_date = date('Y-m-d', strtotime("now + 3 years"));
To Gardenee, Treby and Daniel Lima:
what will happen with 29th February? Sometimes February has only 28 days :)
My solution is: date('Y-m-d', time()-60*60*24*365);
You can make it more "readable" with defines:
define('ONE_SECOND', 1);
define('ONE_MINUTE', 60 * ONE_SECOND);
define('ONE_HOUR', 60 * ONE_MINUTE);
define('ONE_DAY', 24 * ONE_HOUR);
define('ONE_YEAR', 365 * ONE_DAY);
date('Y-m-d', time()-ONE_YEAR);
i want to find out the date after days from the given time.
for example. we have date 29 may 2015
and i want to cqlculate the date after 2 days of 25 may 2015
$Timestamp = 1432857600 (unix time of 29-05-2015)
i have tried to do it with following code but it is not working
$TotalTimeStamp = strtotime('2 days', $TimeStamp);
Missed the + - strtotime('2 days', $TimeStamp); .
Add the + to + 2 days.
Use date & strtotime for this - You can try this -
echo date('d-m-Y',strtotime(' + 2 day', strtotime('2015-05-16')));
$Timestamp & $TimeStamp are not same(may be typo). For your code -
$Timestamp = strtotime(date('Y-m-d'));
$TotalTimeStamp = strtotime('+ 2 days', $Timestamp);
echo date('d-m-Y', $TotalTimeStamp);
Php does have a pretty OOP Api to deal with date and time.
This will create a \DateTime instance using as reference the 25 May 2015 and then you can call the modify method on that instance to add 2 days.
$date = new \DateTime('2015-05-25');
$date->modify('+2 day');
echo $date->format('Y-m-d');
You may find this resource useful:
http://code.tutsplus.com/tutorials/dates-and-time-the-oop-way--net-35395
You can also just add seconds to your timestamp if you have a timestamp ready:
$NewDateStamp = $Timestamp + (60*60*24 * 2);
In the above, sec * min * hours = day -- or 86400 seconds. * 2 = 2 days.
In PHP 5 you can also use D
<?php
$date = date_create('2015-05-16');
date_add($date, date_interval_create_from_date_string('2 days'));
echo date_format($date, 'Y-m-d');
?>
OR
<?php
$date = new DateTime('2015-05-16');
$date->add(new DateInterval('2 days'));
echo $date->format('Y-m-d') . "\n";
?>
I am working on date and I am stuck at a point. How can I get the date after 1.2 or after 1.5 year from the given date?
My code is as follows:
$date = date("Y-m-d", strtotime($from_date . ' +'.$valid_duration.' '.$day) );
where $valid duration can be number as 1, 2, 1.2, etc. and $day is year, months, days.
To get the future date try this:
$StartingDate = date('Y-m-d'); // todays date as a timestamp
$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($StartingDate)) . " + 1 year 2 months 5 days"));
Hope this helps.
Not sure if you can define years/months/days with a decimal point.
But regardless, have you considered just using the timestamp?
86400 seconds in a day, so say you wanted to get date from one year in the future, you could use something like:
$thetime = time() + (365 * 86400);
$date = date("Y-m-d", $thetime);
I have datestamps in the format YYYY-mm-dd HH:mm:ss
Is there an easy way to increase or decrease this by a set amount (say 1 hour or 1 day)?
$plus_day = date('Y-m-d H:i:s', strotime($your_time . " + 1 day"));
$plus_hour = date('Y-m-d H:i:s', strotime($your_time . " + 1 hour"));
Another option is to work with the DateTime class, which has a variety of ways of manipulating the date/time.
$incremented = date_create($date)->modify('+1 day')->format('Y-m-d H:i:s');
// Same as above
// $incremented = date_create($date . ' +1 day')->format('Y-m-d H:i:s');
// $incremented = date_create($date)->add(new DateInterval('P1D'))->format('Y-m-d H:i:s');
// $date = new DateTime($date); $date->modify('+1 day'); $incremented = $date->format('Y-m-d H:i:s');
See MySQL DATE_ADD function:
SELECT DATE_ADD(date, INTERVAL 1 HOUR) FROM tablename
Update
See #genesis' answer for the PHP way. Which you choose is up to whether you want to do the interval in mysql or php (I suspect PHP is the best option)
I have a fixed date from MySql
startDate = 07/03/2011
I wanted to add 60 days on top this date to have an endDate.
$startDate = $result['startDate'];
$endDate = ??? + strtotime("+60 days");
echo $endDate;
From my research, I know it has something do with strtotime, but all the sites I come across with based the start date from current workstation's time. My date is already fixed and entered prior to running and getting the endDate.
Help? Thanks in advance!
In addition to PHP solutions others are providing, you can create the endDate right inside of MySQL and save yourself some of the trouble:
SELECT startDate, DATE_ADD(startDate, INTERVAL 60 DAY) AS endDate FROM table;
-- Or by months (not exactly the same thing)
SELECT startDate, DATE_ADD(startDate, INTERVAL 2 MONTH) AS endDate FROM table;
Relevant documentation here...
You could reformat the results of strtotime()
$startDate = $result['startDate']; // 07/03/2011
$endDate = date("m/d/Y", strtotime("$startDate +60 days"));
Demo: http://codepad.org/9rWnoeQb
$startDate = "07/03/2011";
$endDate = strtotime("+60 days",time($startDate));
$formatted = date('m/d/Y',$endDate);
echo $endDate . "<br/>" . $formatted;
86400 seconds in a day, times number of days.. and add it to current time.
$nextMonth = time()+86400*60;
echo date("Y-m-d H:i:s", $nextMonth);