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);
Related
Lets take any month be of 30 days, then for the date 25/02/2015 when we add 10 days it becomes 5/03/2015 but there are months which are of 29 and 31 days as well.If a month be of 31 then adding 10 days to 25/04/2015 would be 04/05/2015 not 05/05/2015.Does date functionality of MySQL ,PHP and Carbon function of laravel produce correct date after addition or substraction by detecting total days of particular months strictly as per calendar or just assumes every month to be of 30 days?What are the best tools for correct date manipulation?
PHP, yes.
<?php
$date=date_create("2015-02-25");
date_add($date, date_interval_create_from_date_string("10 days"));
echo date_format($date,"Y-m-d");
// echos 2015-03-07
date_add($date, date_interval_create_from_date_string("-10 days"));
echo date_format($date,"Y-m-d");
// echos 2015-02-25
MySQL, yes.
DATE_ADD(SomeDate, INTERVAL 10 DAY)
DATE_SUB(SomeDate, INTERVAL 10 DAY)
You can use like this to get current date,previous date and next date
<?php
$date = date('Y-m-d');
$prev_date = date('Y-m-d', strtotime($date .' -1 day'));
$next_date = date('Y-m-d', strtotime($date .' +1 day'));?>
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 want to get all records from now between a year ago but it's not working exactly.
Made in CodeIgniter
This is my query , antything wrong with it ? :
$currentDate = date("Y-m-d H:m:s");
$yeardate=date('Y-m-d H:m:s', strtotime('-1 year'));
$this->db->select('TimeStamp');
$this->db->where('Transaction', 'COMPLETED');
$this->db->where('TimeStamp>=',$yeardate);
$this->db->where('TimeStamp<=',$currentDate);
$query = $this->db->get('R_Logs');
$results = $query->result();
Thanks in advance
I forgot to add a space between TimeStamp <= and TimeStamp >= .
My bad.
I think it might be easier to try Epoch time and then query off of that - e.g. Get epoch for a specific date using Javascript
That will convert the date to number of days since January 1, 1970. then from that you can subtract 365 days ( or use the yearInteger %4 trick to find out if it is a leap year)
Change these two lines any try once:
$currentDate = date("Y-m-d H:i:s");
$yeardate = strtotime(date('Y-m-d H:i:s') . ' -1 year');
Dump your query and debug:
echo $this->db->last_query();
I want to compare my system date from the date from MySQL database and the comparison should be like if the system date has the diffrence of 10 days than the date from my table's date.
For e.g. my sytem date: 2013-7-31 and the table's date is 2013-8-10 it should echo a msg "10 days left for renewal."
I know how to add days into a date in php but I have no idea of how should I compare these two.
$mydt = date("Y-m-d");
$alertday = date('Y-m-d', strtotime($mydt. " + 10 days"));
Use date_diff()-> DateTime::diff
$interval = date_diff($date1, $date2); //$date1, $date2 are DateTime objects.
echo $interval->d; //Outputs number of day difference.
More functionality can be found here:
http://www.php.net/manual/de/class.dateinterval.php
http://www.php.net/manual/de/datetime.diff.php
in your query let say you pass in the value from $now = strttotime("now");
you can then do the following in MySQL
WHERE DATE_FORMAT($now "%Y-%m-%d") = DATE_FORMAT(DATE_ADD(DATE_FIELD,INTERVAL -10 DAY), "%Y-%m-%d")
Just convert the db table time to a normal unix timestamp subtract and divide by number seconds in a day
$now = time();
$dbtime = strtotime($tabledate);
$daysleft = ($now-$dbtime) / ( 60*60*24);
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');