I've rarely touched PHP date functions,
and now I need to do the follows:
get current date,
get date of three days later
get date of three weeks later
get date of three month later
get date of three years later
and finally to implement such a function:
function dage_generate($number,$unit)
{
}
$unit can be day/week/month/years
http://uk.php.net/strtotime can do most of that:
strtotime("today")
strtotime("+ 3 days")
strtotime("+ 3 weeks")
strtotime("+ 3 months")
strtotime("+ 3 years")
The function would be something like:
function dage_generate($number,$unit)
{
return strtotime("+ ".$number." ".$unit);
}
http://us.php.net/manual/en/function.date.php
Note towards the bottom of the page:
Example #3 date() and mktime() example
<?php
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
?>
Using strtotime() you can do this easily.
$now = time();
$threeDays = strtotime("+3 days");
$threeWeeks = strtotime("+3 weeks");
$threeMonths = strtotime("+3 months");
$threeYears = strtotime("+3 years");
Each of those variables will be an integer which represents the unix timestamp at that point in time. You can then format it into a human readable string using date().
echo date('r', $threeWeeks);
// etc...
Use date_create() to create a DateTime object, then use the add method.
See the examples on the page for the add() method, they contain all you need.
PHP Date/Time Functions with examples
Related
I have some data that makes use of date("j/n/y") format i.e date for today is 23/1/15
I have tried
echo strtotime($today);
but this does not give me the timestamp i want.How would i convert a date in date("j/n/y") format to epoch?.
Use DateTime::createFromFormat() to read the date format and then use DateTime::getTimestamp() to format it as a timestamp.
$date = DateTime::createFromFormat('j/n/y', '23/1/15');
$epoch = $date->getTimestamp();
I think you're looking for the mktime function in PHP. It goes a little like this:
$timestamp = mktime(0,0,0,0,0,0);
Where, in order, the arguments are: hour, minute, second, month, day, year. So, in your case:
$today = mktime(0, 0, 0, 1, 23, 2015);
// Would return the timestamp for Jan. 23rd, 2015 at 12:00:00 am (I think)
If you're looking for a dynamic right now timestamp, you may use date() in each of the arguments of mktime. For example:
$rightnow = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
// Would return the timestamp for Jan. 23rd, 2015 at 10:57:25 am.
But, as John Conde says, it requires you break apart the date before you can use it, so it may not be as efficient.
Hope that helps!
Just to have another approach this one would be good for 85 more years.
$date = date('j/n/y', time());
list($day, $month, $year) = explode("/", $date);
$date = "20" . $year . "-" . $month . "-" . $day;
echo date('m/d/Y', strtotime($date));
I am letting a user input a date. How should I return a date from their input and add 3 years to it? I tried to use mktime and date but it did not work out very well.
$input_date = 2010-03-28
My solution currently is just basic math for a given date 3 years ahead.
$input_date = $input_date + 3000;
Let's say I would want to give a date 4 years and 4 months 10 days
$future_date1 _datum = mktime(0, 0, 0, date("m")-2, date("d"), date("Y")+3);
$future_date2 = mktime(0, 0, 0, date("m"), date("d"), date("Y")+3);
You can use strtotime('+ 3 years') or DateInterval for an object oriented approach.
Use the strtotime function:
$input_date = '2010-03-28';
$future_date = strtotime("+3 years", strtotime($input_date);
http://www.php.net/manual/en/function.strtotime.php
Returns a timestamp, if you want to return YYYY-mm-dd: $future_date = date("Y-m-d", $future_date);
DateTime() offers multiple ways to do this. It's the way PHP recommends doing date math.
You can use DateTime::modify():
$date = new DateTime($input_date);
$date->modify('+3 months');
echo $date->format('Y-m-d');
// one-liner
echo (new DateTime($input_date))->modify('+3 months')->format('Y-m-d');
or with DateInterval()
$date = new DateTime($input_date);
$date->add(new DateInterval('P3M'));
echo $date->format('Y-m-d');
// one-liner
echo (new DateTime($input_date))->add(new DateInterval('P3M'))->format('Y-m-d');
I'm trying to create an date with
$date_end = mktime(0, 0, 0, date('m'), date('d')+7, date('Y'), $date_set);
The output is today + 7 days instead of the date given + 7.
The manual says nothing about mktime() taking a date as argument.
Use strtotime("+7 days", $date_set).
$date_end = mktime(0, 0, 0, date('m', $date_set), date('d', $date_set)+7, date('Y', $date_set));
is, I believe, what you were trying to accomplish (assuming $date_set is a timestamp). Else, #Kristian's suggestion I believe is a good one.
Why are you passing a $date_set variable, and why are you using mktime if you already have the time?
Simply add 7 days: $date_end = $date_set + (7 * 86400);
what is the best way to take a UTC timestamp (integer) and parse it for only the year, month, and day in PHP? Thanks
Use the PHP date() function.
<?php
$timestamp = strtotime("2011-9-12 05:48:00");
$year = date('Y', $timestamp);
$month = date('m', $timestamp);
$day = date('d', $timestamp);
$newTimestamp = mktime(0, 0, 0, $month, $day, $year);
DateTime class is pretty good for this kind of task
<?php
// for PHP5.2+ though
var_dump(date_create('2011-9-12 05:48:00')->setTime(0, 0, 0)->format('U'));
// if input is in integer already
$input = 1315806480;
var_dump(date_create("#{$input}")->setTime(0, 0, 0)->format('U'));
<?php
$time = strtotime(date('Y-m-d', *timestamp*));
?>
EDITED:
The date() function takes your timestamp and represents it in a human readable string with only the year, month, and date (i.e. "2014-07-25") and omitting the hour, minutes, and seconds.
Then the strtotime() function takes that human readable string and turns it back into your timestamp, effectively making the time at 00:00 on that day.
Is there a quicker way of creating a date such as:
echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")+3, date("Y")));
Thanks if you can help.
How about strtotime():
date('Y-m-d', strtotime('+3 days'));
You will have to look into strtotime(). I'd imagine your final code would look something like this:
$currentDate = strtotime('today');//your date variable goes here
$futureDate = date('Y-m-d', strtotime('+ 2 days', $currentDate));
echo $futureDate;
Live Demo
If you are using PHP version >= 5.2 I strongly suggest you use the new DateTime object. For example like below:
$futureDate = new DateTime("today");
$futureDate->modify("+2 days");
echo $futureDate->format("Y-m-d");
Live Demo