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).
Related
I want to get difference between 2 timestamps of format Y-m-d H:i:s in minutes in PHP.
The code used is,
$currentDate = date('Y-m-d H:i:s');
$userLastActivity = date($date);
$timeLapse = (($currentDate - $userLastActivity)/60);
Here, $date is obtained from database.
The value of $timeLapse is 0 in output. Please help.
Try using unix timestamp. Practically it measures the time in seconds from 1/1/1970 and it's a lot easier to use and understand than a php object.
$currentTimestamp = new DateTime()->getTimestamp();
$userLastActivity = date($date)->getTimestamp();
$timeLapse = (($currentDate - $userLastActivity)/60);
You should have the time saved as timestamp on the server too, in that case you could use the $date directly as a number, with no need for a conversion. And also, because it's universal, you can pass it around to javascript or any other language without any worries for conversion
Use strtotime to parse textual datetime into a Unix timestamp and substract $userLastActivity from $currentDate and divide by 60.
See if this helps -
<?php
$currentDate = strtotime(date('Y-m-d H:i:s'));
$date = "2016-10-11 02:40:50";
$userLastActivity = strtotime($date);
echo round(abs($currentDate - $userLastActivity) / 60). " minutes";
?>
For more details :strtotime
Change these 2 lines for a start, as I don't think dividing dates by 60 is gonna work to well.
$currentDate = time();
$userLastActivity = strtotime($date);
That way you have time stamps and not dates (string)
I want to update a unix timestamp and add x months.
This is a timestamp that i use 1456256866
strtotime("+1 month")
what i want to accieve is :
$time = '1456256866';
//update $time with x months something like
$time("+5 month");
Can someone put me in the right direction?
Much Thanks
You could do something like below. the function strtotime takes a second argument.
$time = 1456256866;
$time = strtotime('+5 month', $time);
For such operations You should use Datetime class, especially Add method:
$date = new DateTime('#1456256866');
$date->add(new DateInterval('P5M'));
echo $date->format('Y-m-d') . "\n";
Check more here: http://php.net/manual/pl/datetime.add.php
I have the following code to get the current date and hour:
date_default_timezone_set('Asia/Kabul');
$today = date('Ymdh');
echo $today; // prints 2015020212 Fine
I was wondering if I could get it's number of seconds! But the following does not give me that.
echo strtotime($today);
I really need to get date 2015020212 in seconds! please help me!
The reason why strtotime is not returning what you expect is because you need to give it the date in the correct format:
$today = date('Y-m-d');
strtotime($today);
and
$today = date('Y-m-d H:i:s');
strtotime($today);
will work, but you have given it Ymdh, which PHP has no idea how to interpret.
For more info on valid formats check out the page in the PHP manual.
You can use date('Ymdh h:i:s') if you're looking for the local time as well.
h would give the 12 hour format of the hour with leading zeroes;
i for minutes; and
s for seconds.
Source : PHP Date Manual
You will probably need something like this:
$date = new DateTime();
$time_in_seconds = $date->getTimestamp();
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);
}
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.