![]() |
Php date problem 'Y' displying 1969
Why is this code displaying 'Y' as 1969? :helpme
Quote:
|
Because you have given the mktime() function a negative number for the year value.
|
Quote:
Or just remove the '0' for the year? |
No, you need to understand what mktime() does and what parameters it expects.
https://www.php.net/manual/en/function.mktime.php You are giving it a value for the day where it expects a value for the year. |
It would be far easier to work with timestamps for this.
Code:
echo date("M d Y", time() - (86400 * 8)); |
Quote:
|
Quote:
how about when your $today-8 is an invalid day value? on the 7th $day-8 comes back as -1 also what is with splitting the date into pieces like that? you were doing that in the database table at one point too. you are taking your date and converting it to 3 integers and then turning it back into a date, why? every language has date functions to handle date calculations, php included https://www.w3schools.com/php/php_ref_date.asp handling it the way you are doing is basically trying to reinvent the wheel Quote:
i prefer to use datetime objects in php you can add and subtract from a date pretty easily using the datetime object and associated functions $mydate=date_create(date('Y-m-d',time())); // create the datetime object date_sub($mydate, date_interval_create_from_date_string("8 days")); // subtract 8 days echo date_format($mydate,"M d Y"); // display the result https://www.w3schools.com/php/func_date_date_sub.asp you could also use date_modify() for this $date=date_create(date('Y-m-d',time())); date_modify($date,"-8 days"); echo date_format($date,"Y-m-d"); https://www.w3schools.com/php/func_date_modify.asp you should, at all times while working on this stuff have a browser window open to php.net and w3schools.com or other coding sites, just my opinion . |
sarettah you are definitely a case study in patience :thumbsup
|
Quote:
. |
date('M d Y',strtotime("-8 days"));
|
The code is displaying 'Y' as 1969 because the third argument passed to the mktime() function is $today-8 instead of $thisYear.
Here is the corrected code: Code:
<?php |
Thank you all for the assistance with this i genuinely appreciate the advice and feedback :)
|
It took maybe 30 seconds to copy the post just as it was written, paste it into ChatGPT, wait for the answer, copy that, come to GFY, and paste it.
Learn to use AI or you're getting left behind. |
All times are GMT -7. The time now is 04:24 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123