View Single Post
Old 01-13-2023, 07:23 AM  
sarettah
I'll make you famous
 
Industry Role:
Join Date: Oct 2002
Posts: 13,941
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


.
sarettah is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote