This question already has answers here:
How to save dates older than timestamp in PHP
(3 answers)
Closed 3 years ago.
I am looking to have a one line return function to get the date 150 years ago from today.
I currently have,
return date('Y-m-d', strtotime('-150 year'));
The problem is this returns 1970-01-01 instead of 150 years ago.
Why is this?
Your issue is that strtotime returns a unix timestamp (seconds since 1970-01-01 00:00:00), so the earliest date it can understand is 1970-01-01. Since 150 years ago today is before 1970-01-01 strtotime is returning false, which is interpreted as a 0 timestamp by date, resulting in an output of 1970-01-01. To work around this limitation use the DateTime class:
$date = new DateTime('-150 year');
echo $date->format('Y-m-d');
Output (as of 2020-02-19):
1870-02-19
If the code does need to be in one line, you can write:
$date = (new DateTime('-150 year'))->format('Y-m-d');
Demo on 3v4l.org
Related
This question already has answers here:
PHP Strtotime without current time?
(2 answers)
Closed 4 months ago.
I have an Unix timestamp like this 1660293621 (2022-08-12 8:40). I want to get next 2 days not counting current date. I expect the result to be 2022-08-15 00:00.
I tried
strtotime("+3 Days", $current_date)
but it returns 2022-08-15 8:40, not 00:00
How can I get that in PHP? Thank you~
$Today=date('y:m:d');
// add 3 days to date
$NewDate=Date('y:m:d', strtotime('+3 days'));
Reference:
Increase days to php current Date()
I figured it out, just add 0:00 will help
$next2days = strtotime("+3 days 0:00", $current_date);
This question already has answers here:
PHP strtotime for June returns July
(3 answers)
Closed 2 years ago.
Converting iso string in zulu time returns the next time. I'm not sure what I've done wrong please. see example below
$dateTime = "2020-04-31T23:59:58Z"
(new DateTime($dateTime)); // returns 2020-05-01 23:59:58.0 +00:00
April has 30 days.
As such, DateTime returns "April 31st" as May 1st (April 30th + 1 day).
This question already has answers here:
PHP Adding 15 minutes to Time value
(9 answers)
Closed 4 years ago.
I want to add a specific period like 2 hours to only time.
BUT only TIME. There will be no date related issue.
Like my time is 02:00:00
And I want to add 1 hour to this time.
So, the result will be 03:00:00
Is there any PHP built-in function like strtotime()?
I don't want explode related function for this like explode(":", $time)
Is it possible? Please help someone.
Try
$new_time = date("H:i:s", strtotime("+ 1 hours"))
or if you would just like the timestamp:
strtotime("+ 1 hours")
This question already has answers here:
Subtract one second from a given time
(2 answers)
Closed 8 years ago.
I writing a code for subtract seconds from a time using php. i have date which assigned to variable , i need to subtract seconds from that date.
$date="2014-03-16 17:40:27";
echo date("Y-m-d H:i:s", strtotime($date) - strtotime("-600 seconds"));
but this gives me dates on 1970S, i search everhere and didn't found a answer which matched for my question. can anyone help me to fix this little code
strtotime() gives you a timestamp in seconds. Don't make another timestamp to subtract from it, just take 600 from it:
echo date("Y-m-d H:i:s", strtotime($date) - 600);
//2014-03-16 17:30:27
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP How to find the time elapsed since a date time?
I have the following which produces MONTH YEAR.
<?php echo date('F Y', strtotime($video['Video']['posted_date'])); ?>
I'd like this to output to "Time" ago. i.e 3 hours ago, 6 months ago, 2 years ago.
Any help will be very much appreciated.
Thanks
Andy
$now = new DateTime(date("Y-m-d H:i:s"));
$videoPosted = new DateTime($video['Video']['posted_date']);
$interval = $now->diff($videoPosted);
echo $interval->format('%h hours, %m month and %d days ago');
Use only if PHP version > 5.3
If you use php 5.3+, you are looking for DateInterval::format()
this will help you to calculate date difference: How to calculate the difference between two dates using PHP?
and after that you can convert the result using strtotime()