i want to have a date 6 years from now?
how do i do that?
<?php
$timestamp = strtotime('+6 years');
echo date('Y-m-d H:i:s', $timestamp);
?>
date_default_timezone_set('America/Los_Angeles'); //required if not set
$date = new DateTime('1/1/1981');
$date->modify('+60 year');
echo $date->format('Y-m-d');
Above is not affected by unix time stamp date range (before 1970 and after 2038).
Also you can directly compare dates with Comparison operators directly, no need convert them to Time stamp.
Requires PHP 5.3
strtotime('+6 years');
you can pass that timestamp into something like strftime();
strtotime
Still laughing about ChaosPandion's comment :)
echo strtotime ("+6 years");
should do the trick.
Your description isn't very precise, but echo date("Y-m-d", strtotime("+6 years")); might be what you need ...
189302400 is the number of seconds in 6 years.
Get the current timestamp, then add 189302400, and then convert the timestamp to a date string.
Related
just trying to create a variable where it outputs the exact current datetime and adding exactly one additional year. how do i add the days?
$expirationdate = date('Y-m-d H:i:s', .' + 1 year'));
Lets say the exact current date is 2019-01-23 17:11:25
the variable will be: 2020-01-23 17-11-25 (+365)
Also, if a person manually modifies the date on their PC/Phone, will that time be the start of the current date on the variable?
You can achieve your result by using strtotime() and date() function of php
$expirationdate = date('Y-m-d H:i:s', strtotime('+ 1 year'));
print_r($expirationdate);
You can read more about the strtotime() and date()
Try with below code:
$expirationdate = date('Y-m-d H:i:s', strtotime('+1 years'));
I hope it will help you.
Try This :
$date= date('Y-m-d H:i:s',strtotime('+1 years'));
I think the best way to work with dates and time in PHP is through the DateTime object. You can use modify method to add or subtract days, years or whatever you want, like this:
$d = new DateTime(); //current date
$d->modify('+10 days');
$d->modify('+1 year');
echo $d->format('Y-m-d H:i:s');
Regarding your second question if a person modifies date on his computer it won't change anything because PHP runs on the server and takes date from it (not from the user machine).
I want to get the timestamp of a day/time for eg
17/12/2014 8pm
Currently I am doing
$curtime = strtotime(date("Y-m-d H:i:s"));
which is giving me the current timestamp but I need to be of 8pm.
Any help is highly appreciated. Thanks in advance.
If you're trying to get a timestamp of today at 8pm, it's actually much more simple than using date since you can use relative times in a strtotime:
$curtime = strtotime('today 8pm');
If you test this with date:
echo date('Y-m-d H:i:s', $curtime); // Returns 2014-12-17 20:00:00
Here's a whole writeup on relative date formats that explains how you can construct proper relative dates.
The best way to do this is using date_create_from_format. Checking out format's parameters, you will end up with something like this:
$date = date_create_from_format('d/m/Y ga', '17/12/2014 8pm');
if (!empty($date)) {//returns false if can't create date
$timestamp = $date->getTimestamp();
//echo date('d/m/Y H:i:s', $timestamp);
}
Im trying to add a certain amount of days to a timestmp using this in PHP:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo $endDate2;
but its displaying: 1216526400
any ideas?
Try:
echo date("Y-m-d H:i:s",$endDate2);
Or (for just the date):
echo date("Y-m-d",$endDate2);
You can find documentation about how to format your string here: http://php.net/manual/en/function.date.php
You should be using DateTime for working with dates. It's timezone friendly.
$datetime = new DateTime('2008-06-20');
$datetime->modify('+1 day');
echo $datetime->getTimestamp();
strtotime() converts the date into a unix timestamp which is the number of seconds since January 1st 1970. If you want a date output you have to run the finished timestamp through date() first.
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate.' +1 day');
echo date("Y-m-d", $endDate);
strtotime creates a Unix timestamp so if you want to be presented with a formatted date, you need to pass the timestamp as an argument to the date function as follows:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo date('Y-m-d', $endDate2);
Additionally, there are a wide variety of parameters you can use in the date function if you want to display additional information.
e.g.: echo date('Y-m-d H:i:s', $endDate2); or echo date('Y-m-d h:i:s a', $endDate2);, etc.
Sooooo close, just take your timestamp and convert it back into date format using date("desired format",$endDate2);
DateTime is a very nice way to deal with dates. You can try like this:
$capturedDate = '2008-06-20';
$date = DateTime::createFromFormat('Y-m-d', $capturedDate)->modify('+1 day');
echo $date->getTimestamp();
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
increment date by one month
I have the following code to sum dates:
$var1 = date("d/m/Y");
$dia = substr($var1,0,2);
$mes = substr($var1,3,2);
$a_o = substr($var1,6,4);
$date = $a_o."-".$mes."-".$dia;
$new_date = strtotime( "+3 month", strtotime($date));
$new_date = date('d/m/Y', $new_date);
echo $new_date;
That is the normal method I found on php.net but when I tried to change $var1 with another date, for example, '21/07/2020' the result is '31/01/1970', I don't know what's wrong. I tried many forms but the result is the same.
How can I fix this? Or what other forms exist to add X months to a date?
Look into the date_add() function that was added for 5.3.0.
DateTime::add -- date_add — Adds an amount of days, months, years,
hours, minutes and seconds to a DateTime object
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?>
There is also the date_modify() function that was added for 5.2.0.
DateTime::modify -- date_modify — Alters the timestamp
<?php
$date = new DateTime('2006-12-12');
$date->modify('+1 day');
echo $date->format('Y-m-d');
?>
You're going about it completely wrong. date() gives you a nicely formatted STRING. If you want to do math with dates, you need to work with the native PHP date/time values, which are simple integers - unix time stamps.
You're taking that unix timestamp (the default value in date()), converting it to a string, taking apart that string, recombining the string in a different order, then converting it back to a timestamp, then converting the timestamp into a different formatted string. That's like going on a 500mile road trip so you can cross the street.
Any reason you can't simply do:
$new_date = date('d/m/Y', strtotime('+3 month'));
and eliminate everything else in your code snippet?
In your place, I'll trasform the date in timestamp, then I'll add the new date in seconds (i.e. 3 months are 60 * 60 * 24 * 90 = 7 776 000 secs.) and then transform back in date :P
How can I get what date it will be after 31 days starting with $startDate, where $startDate is a string of this format: YYYYMMDD.
Thank you.
strtotime will give you a Unix timestamp:
$date = '20101007';
$newDate = strtotime($date.' + 31 days');
you can then use date to format that into the same format, if that's what you need:
echo date('Ymd', $newDate);
If you're using PHP 5.3:
$date = new DateTime('20101007');
$date->add(new DateInterval('P31D'));
echo $date->format('Y-m-d');
The pre-5.3 date functions are lacking, to say the least. The DateTime stuff makes it much easier to deal with dates. http://us3.php.net/manual/en/book.datetime.php
Just a note that +1 month will also work if you want the same date on the next month and not 31 days exactly each time.
echo date('Y m d',strtotime('+31 Days'));