GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Tech Php date problem 'Y' displying 1969 (https://gfy.com/showthread.php?t=1360918)

Publisher Bucks 01-13-2023 03:59 AM

Php date problem 'Y' displying 1969
 
Why is this code displaying 'Y' as 1969? :helpme

Quote:

<?php

$today = date("d");
$thisMonth = date("M");
$thisYear = date("Y");
echo date("M d Y", mktime(0,0,0, $thisMonth, $today, $today-8));

?>
I'm trying to get it to display: Jan 5 2023 (minus 8 days from today).

redwhiteandblue 01-13-2023 04:50 AM

Because you have given the mktime() function a negative number for the year value.

Publisher Bucks 01-13-2023 04:51 AM

Quote:

Originally Posted by redwhiteandblue (Post 23084132)
Because you have given the mktime() function a negative number for the year value.

So I need to take it out of the echo statement and display it within its own echo statement?

Or just remove the '0' for the year?

redwhiteandblue 01-13-2023 04:57 AM

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.

redwhiteandblue 01-13-2023 05:00 AM

It would be far easier to work with timestamps for this.

Code:

echo date("M d Y", time() - (86400 * 8));

Publisher Bucks 01-13-2023 05:38 AM

Quote:

Originally Posted by redwhiteandblue (Post 23084136)
It would be far easier to work with timestamps for this.

Code:

echo date("M d Y", time() - (86400 * 8));

Okay thanks for the suggestion and assistance, I appreciate it :thumbsup

sarettah 01-13-2023 07:23 AM

Quote:

<?php

$today = date("d");
$thisMonth = date("M");
$thisYear = date("Y");
echo date("M d Y", mktime(0,0,0, $thisMonth, $today, $today-8));
?>
this is wrong for so many reasons beyond just having your mktime parameters screwed up

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:

echo date("M d Y", time() - (86400 * 8));
that is a reasonable solution


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


.

fuzebox 01-13-2023 11:54 AM

sarettah you are definitely a case study in patience :thumbsup

sarettah 01-13-2023 07:47 PM

Quote:

Originally Posted by fuzebox (Post 23084301)
sarettah you are definitely a case study in patience :thumbsup

I try



.

k0nr4d 01-14-2023 04:21 AM

date('M d Y',strtotime("-8 days"));

Mr Pheer 01-14-2023 04:47 AM

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

$today = date("d");
$thisMonth = date("m");
$thisYear = date("Y");
echo date("M d Y", mktime(0,0,0, $thisMonth, $today-8, $thisYear));

?>

This will display the date 8 days ago in the format "Jan 6 2023".

Publisher Bucks 01-14-2023 04:54 AM

Thank you all for the assistance with this i genuinely appreciate the advice and feedback :)

Mr Pheer 01-14-2023 04:57 AM

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