Below is a snippet of code that is part of a much bigger routine, although all I need assistance with is changing the code so it is uses the prior month, or the ability to define the month.
When this executes it adds data to MySQL based the current month, i.e. February but I need to have it also add data (once) for January.
Any help would be great.
$now = new DateTime('now');
$thisYear = $now->format("Y");
$thisMonth = $now->format('m');
$bookingsSql = str_replace('{check_in_year}', $thisYear, $bookingsSql);
$bookingsSql = str_replace('{check_in_month}', $thisMonth, $bookingsSql);
$bookingsSql = str_replace('{check_out_year}', $thisYear, $bookingsSql);
$bookingsSql = str_replace('{check_out_month}', $thisMonth, $bookingsSql);
$bookingsSql = str_replace('{close_date_year}', $thisYear, $bookingsSql);
$bookingsSql = str_replace('{close_date_month}', $thisMonth, $bookingsSql);
You can specify "last month" as your argument when creating your DateTime
object.
$myDate = new DateTime ("last month");
print "Date: {$myDate->format("d")}, month: {$myDate->format("m")}
";
// Output: Date: 14, month: 01
Alternatively, you can also use DateTime::sub()
to alter the date you have.
$myToday = new DateTime ();
$lastmonth = $myToday->sub (new DateInterval ('P1M'));
print "Last month: {$lastmonth->format("m")}
";
// Output: Last month: 01
You can also define your own date if you like. See the DateTime() manual.