How do I use PHP to get the current year?
You can use either date or strftime. In this case I'd say it doesn't matter as a year is a year, no matter what (unless there's a locale that formats the year differently?)
For example:
<?php echo date("Y"); ?>
On a side note, when formatting dates in PHP it matters when you want to format your date in a different locale than your default. If so, you have to use setlocale and strftime. According to the php manual on date:
To format dates in other languages,
you should use the setlocale() and
strftime() functions instead of
date().
From this point of view, I think it would be best to use strftime as much as possible, if you even have a remote possibility of having to localize your application. If that's not an issue, pick the one you like best.
get current year and five years before
The second statement in your for
loop need to be the "end" condition.
Change the operator =
(assignation) to <=
(comparison):
<select class='selrange' id='rangeyeara'>
<?php
for($y=date("Y")-5; $y<=date("Y"); $y++){
echo "<option>" . $y . "</option>";
}
?>
</select>
How to set current year and previous year in variables using php?
You use Carbon.
Documentation :- http://carbon.nesbot.com/docs/
$date = Carbon\Carbon::createFromFormat('d/m/Y', $date);
$date->subYear();
echo $date->format('d/m/Y'); // Previous year
How to get a specific date in the current year
You can just use:
date("Y-06-30")
How to get the year number with PHP?
Look at the functions date() and strtotime():
echo date('Y');
echo date('Y', strtotime('+1 year'));
PHP Get Current year after current month
// incoming month
$month = 'November';
$monthNumber = date("n", strtotime($month));
// current month
$currentMonth = date("n");
if ($currentMonth >= $monthNumber) {
echo $month . ' ' . date('Y', strtotime('+1 year'));
} else {
echo $month . ' ' . date('Y');
}
So I am converting the incoming and current month to number format, checking if current month is bigger or equal than incoming and then based on that, I decide if it should be next year.
DateTime object How to change the year to be the current year
You can either use modify()
to increment/decrement to current year or use setDate()
like this:
$dob->setDate((int) date("Y"), (int)$dob->format('m'), (int)$dob->format('d'));
Related Topics
How to Use an Object Method as a Callback Function
How to Protect My Source Code When Deployed
Parse Error: Syntax Error, Unexpected '' (T_Encapsed_And_Whitespace)
Supplied Key Param Cannot Be Coerced into a Private Key with Google APIs
Mysql_Fetch_Array Returns Duplicate Data
Parse Math Operations with PHP
Storing Time Information: Timezone Required
Populate Select Drop Down from a Database Table
PHP Include File in Webroot from File Outside Webroot
Printing Content of a Xml File Using Xml Dom
How to Enable Put Requests in Azure
Multi Checkbox Fields in Woocommerce Backend
Select MySQL Rows But Rows into Columns and Column into Rows
How to Get Value from Form Without Submitting It