this is a very lame question but i m not able to find this one.
How to get today's date and date after two months..
format is month-date-year (numerical.)
You can use the strtotime() function :
$today = time();
$twoMonthsLater = strtotime("+2 months", $today);
// If what you really want is exactly 60 days later, then
$sixtyDaysLater = strtotime("+60 days", $today);
// ...or 8 weeks later :
$eightWeeksLater = strtotime("+8 weeks", $today);
In any case, the resulting new timestamps can then be converted to month-date-year :
echo 'Today is : ' . date('m-d-Y', $today);
echo 'Two months later will be : ' . date('m-d-Y', $twoMonthsLater);
** UPDATE **
From the PHP manual
Note: Please keep in mind that these functions are dependent on the locale settings of your server. Make sure to take daylight saving time (use e.g. $date = strtotime('+7 days', $date) and not $date += 7*24*60*60) and leap years into consideration when working with these functions.
Just thought I should mention it...
There are a couple of ways you could go about it - the first one would be to do something like this:
echo $currentdate = date("Y-m-d H:i:s",time());
echo $after60days = date('Y-m-d H:i:s', time() + 60 * 60 * 24 * 60);
Basically, you take the current timestamp, expressed in seconds, and add 60 * 60 * 24 * 60, which is the amount of seconds in two months.
Another way to do it, which is my preferred way and how I would do it, is this:
$after60days = strtotime("+60 days");
The outcome will be exactly the same, $after60days will have a value equal to the timestamp of the day exactly two month from now, but it uses PHP's own strtotime() function.
Of course, if you need to output a date in a format that easy to read for humans, you can do something like this:
echo date('Y-m-d H:i:s',$after60days);
Today:
date('m-d-Y', time());
Two months from now:
date('m-d-Y', time() + (86400 * 60));
Related
I have a date formatted like this: Ymd
I can't seem to find a way to be able to add a number of weeks to this date, I have tried the following:
$quote_start_date = $job['quote_authorised'];
$newdate = date($quote_start_date, strtotime('+5 weeks'));
However the new date is the same, what is the easiest way to add weeks to a date formatted like this?
The 2nd parameter to date takes seconds since epoch. Just add 5 weeks in seconds to the time, ie:
$newdate = date($format, strtotime($quote_start_date) + (5 * 7 * 24 * 60 * 60));
Or just use the constant value "3024000"
$newdate = date($format, strtotime($quote_start_date) + 3024000);
The first parameter of date expects a format for your outputted date string. I think you're looking for the following:
$quote_start_date = $job['quote_authorised'];
$newdate = date("Ymd", strtotime('+5 weeks', strtotime($quote_start_date)));
The more efficient approach would be to use a fixed value for the number of seconds in a week and not rely on PHP parsing an additional strtotime function:
$quote_start_date = $job['quote_authorised'];
$newdate = date("Ymd", strtotime($quote_start_date) + 3024000);
Its a demo :
$date = new Date();
$nextDate = new Date($date.setTime( $date.getTime() + 1 * 86400000 ));
// here 1 is number of 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 add time to an existing date. I have 2 string variables:
$date = "2013-01-05 10:55:15";
$interval = "50:25:10";
I want to calculate the final date "2013-01-07 13:20:25". The hours in time can be bigger than 23, meaning that this interval can be greater than a whole day.
What's the best way to do this ?
Use DateTime API:
$date = new DateTime("2013-01-05 10:55:15");
$date->add(new DateInterval("PT50H25M10S"));
then you can convert it back to string with the same date format you would use with date() function, if you want to:
$string = $date->format("Y-m-d H:i:s");
For more information about the DateInterval definition, visit this page:
DateInterval
The format starts with the letter P, for "period." Each duration
period is represented by an integer value followed by a period
designator. If the duration contains time elements, that portion of
the specification is preceded by the letter T.
Here are some simple examples. Two days is P2D. Two seconds is PT2S.
Six years and five minutes is P6YT5M.
so in this case PT50H25M10S means 50 hours, 25 minutes, and 10 seconds
Note that DateInterval is available only since PHP 5.3, if you have to use lower version, you could use something like this:
$time = strtotime("2013-01-05 10:55:15");
$time += 55*60*60 + 25*60 + 10;
$newDate = date("Y-m-d H:i:s");
This is a little tricky.
Normally what you would do here if it was a static period, or was a single period type, is something along the lines of:
$date = "2013-01-05 10:55:15";
$time = new DateTime($date);
$time->add(new DateInterval('PT5M'));
This would add 5 minutes to the datetime. However I doubt you can pass the whole interval in. So what you'll probably have to do is split the interval by : and then add each part of the interval (I assume it is dynamic?) to the date separately. So first hours, then minutes, then seconds
For more on this, see here: http://www.php.net/manual/en/datetime.add.php
You could first explode the interval and then get the hours, minutes, seconds, and then use DateTime's add() to add the interval, like so:
$interval = '50:25:10';
$datestring = '2013-01-05 10:55:15';
list($hours, $minutes, $seconds) = explode(':', $interval);
$date = new DateTime($datestring);
$date->add(new DateInterval('PT'.$hours.'H'.$minutes.'M'.$seconds.'S'));
echo $date->format('Y-m-d H:i:s');
Demo!
Use a DateInterval and DateTime->add():
$date = new DateTime("2013-01-05 10:55:15");
$date->add(new DateInterval("PT50H25M10S"));
In case anyone needs based on answers above, I made my function like this:
public function addTime($date, $time) {
$atime = explode(":", $time);
$_date = date_create_from_format("Y-m-d H:i:s", $date);
$_date->add(new DateInterval("PT" . intval($atime[0])."H".intval($atime[1])."M".intval($atime[2])."S"));
return $_date->format("Y-m-d H:i:s");
}
hope it helps someone
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Given a time, how can I find the time one month ago
How can I print an hour ago in PHP using Date?
$date=date("Y-m-d H:i:s");
$time(-1, now);
$result=$date.$time;
So If I wanted to say "John visited last "
Would print
John visited last 20th Feb 2012, 17.26
$date = date('Y-m-d H:i:s', strtotime('-1 hour'));
echo 'John visited last ' . $date;
$date = date("Y-m-d H:i:s", time() - 3600);
time() -> Current timestamp
Time minus 3600 seconds, is the time 1 hour ago. To get the date formatted, you can look here for the options: http://php.net/manual/en/function.date.php
Alternatively you could use the following format:
$date = date("Y-m-d H:i:s", strtotime('-1 hour'));
Though using that method can be a little clunky if you want to remove more specific units of time (Such as one day and 3 hours).
Thats if I've understood what you want to do correctly that is.
I suppose you would be fetching date and time from mysql and the best thing to do is using mysql's DATE_FORMAT function and work out.
Other wise in simple php you could do it like this
$date=date("Y-m-d H:i:s", $time -3600);
Better option is to use strtotime like this one
$date=date("Y-m-d H:i:s", strtotime('-1 hour'));
And get the work done.
Mmm, search the manual the function I used. You are missing something about PHP date/time functions...
// Get the date string for time() - 3600, that is
// the current time minus 3600 seconds (= 1 hour)
$date = date("Y-m-d H:i:s", time() - 3600);
$result = $date;
I have a unix timestamp for the current time. I want to get the unix timestamp for the start of the next day.
$current_timestamp = time();
$allowable_start_date = strtotime('+1 day', $current_timestamp);
As I am doing it now, I am simply adding 1 whole entire day to the unix timestamp, when instead I would like to figure out how many seconds are left in this current day, and only add that many seconds in order to get the unix timestamp for the very first minute of the next day.
What is the best way to go about this?
The most straightforward way to simply "make" that time:
$tomorrowMidnight = mktime(0, 0, 0, date('n'), date('j') + 1);
Quote:
I would like to figure out how many seconds are left in this current day, and only add that many seconds in order to get the unix timestamp for the very first minute of the next day.
Don't do it like that. Avoid relative calculations whenever possible, especially if it's so trivial to "absolutely" get the timestamp without seconds arithmetics.
You can easily get tomorrow at midnight timestamp with:
$tomorrow_timestamp = strtotime('tomorrow');
If you want to be able to do a variable amount of days you could easily do it like so:
$days = 4;
$x_num_days_timestamp = strtotime(date('m/d/Y', strtotime("+$days days"))));
$tomorrow = strtotime('+1 day', strtotime(date('Y-m-d')));
$secondsLeftToday = time() - $tomorrow;
Something simple like:
$nextday = $current_timestamp + 86400 - ($current_timestamp % 86400);
is what I'd use.
The start of the next day is calculated like this:
<?php
$current_timestamp = time();
$allowable_start_date = strtotime('tomorrow', $current_timestamp);
echo date('r', $allowable_start_date);
?>
If it needs to follow your peculiar requirement:
<?php
$current_timestamp = time();
$seconds_to_add = strtotime('tomorrow', $current_timestamp) - $current_timestamp;
echo date('r', $current_timestamp + $seconds_to_add);
?>
My variant:
$allowable_start_date = strtotime('today +1 day');