How do i get this year in php?
I want to put a copyright notice in the footer of a web site, but I think it's incredibly tacky for the year to be outdated. Show How would I make the year update automatically with PHP 4 or PHP 5?
allenski 1,4254 gold badges20 silver badges35 bronze badges asked Sep 15, 2008 at 15:34
11 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:
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:
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.
Jason 5325 silver badges24 bronze badges answered Sep 15, 2008 at 15:45
Erik van BrakelErik van Brakel 22.8k2 gold badges51 silver badges66 bronze badges 6
Larsenal 48.2k42 gold badges146 silver badges216 bronze badges answered Sep 15, 2008 at 15:35
Daniel PapasianDaniel Papasian 16k6 gold badges29 silver badges32 bronze badges 3 My super lazy version of showing a copyright line, that automatically stays updated:
This year (2008), it will say:
Next year, it will say:
and forever stay updated with the current year. Or (PHP 5.3.0+) a compact way to do it using an anonymous function so you don't have variables leaking out and don't repeat code/constants:
answered Sep 15, 2008 at 22:51
gregmacgregmac 23.7k10 gold badges87 silver badges115 bronze badges 2 With PHP heading in a more object-oriented direction, I'm surprised nobody here has referenced the built-in
or one-liner with class member access on instantiation (php>=5.4):
answered Jan 2, 2013 at 18:03
Thomas KelleyThomas Kelley 10k1 gold badge35 silver badges41 bronze badges 0
Naveed 40.6k32 gold badges95 silver badges130 bronze badges answered Sep 15, 2008 at 15:36
chrisbchrisb 2,1901 gold badge20 silver badges23 bronze badges 0
I love strftime. It's a great function for grabbing/recombining chunks of dates/times. Plus it respects locale settings which the date function doesn't do. answered Sep 15, 2008 at 15:35
Mark BiekMark Biek 143k54 gold badges155 silver badges200 bronze badges This one gives you the local time:
And this one UTC:
Alfred Huang 16.9k32 gold badges114 silver badges185 bronze badges answered Sep 15, 2008 at 15:44
Alexey LebedevAlexey Lebedev 11.8k4 gold badges39 silver badges47 bronze badges Here's what I do:
below is a bit of explanation of what it does:
Y will gives you four digit (e.g. 1990) and y for two digit (e.g. 90) answered Jan 17, 2014 at 15:52
0 For 4 digit representation:
2 digit representation:
Check the php documentation for more info: https://secure.php.net/manual/en/function.date.php
Janyk 5713 silver badges18 bronze badges answered Oct 16, 2014 at 8:02
joan16vjoan16v 4,9393 gold badges47 silver badges48 bronze badges
answered Apr 11, 2014 at 7:08
kkarayatkkarayat 3845 silver badges14 bronze badges use a PHP function which is just called It takes the current date and then you provide a format to it and the format is just going to be Y. Capital Y is going to be a four digit year.
answered Feb 9, 2017 at 20:19
Wael AssafWael Assaf 1,15712 silver badges23 bronze badges
This code should do answered Feb 15, 2017 at 23:22
3 Just write:
Or:
And 'echo' this value...
answered Oct 26, 2016 at 7:02
saadksaadk 1,17512 silver badges17 bronze badges use a PHP and the format is just going to be Y. Capital Y is going to be a four digit year.
answered Dec 28, 2018 at 9:45
Sanu0786Sanu0786 56310 silver badges15 bronze badges If your server supports Short Tags, or you use PHP 5.4, you can use:
answered Aug 30, 2012 at 16:32
PanicGripPanicGrip 1421 silver badge2 bronze badges 2 BTW... there are a few proper ways how to display site copyright. Some people have tendency to make things redundant i.e.: Copyright © have both the same meaning. The important copyright parts are:
or
answered Nov 28, 2016 at 2:07
MilanMilan 3,0711 gold badge32 silver badges44 bronze badges For up to php 5.4+
Or you can use it with one line
If you wanna increase or decrease the year another method; add modify line like below.
answered Dec 25, 2016 at 0:57
Ivan BarayevIvan Barayev 2,0075 gold badges25 silver badges29 bronze badges
answered Jan 3 at 16:50
Mr. CoderxMr. Coderx 4416 silver badges4 bronze badges Get full Year used:
Or get only two digit of year used like this:
Suraj Rao 29.1k11 gold badges95 silver badges100 bronze badges answered Sep 15, 2017 at 5:07
To get the current year using PHP’s date function, you can pass in the “Y” format character like so:
The example above will print out the full 4-digit representation of the current year. If you only want to retrieve the 2-digit format, then you can use the lowercase “y” format character:
The snippet above will print out 20 instead of 2020, or 19 instead of 2019, etc.
Musa Haidari 2,0415 gold badges29 silver badges52 bronze badges answered Feb 1, 2021 at 8:31
1 My way to show the copyright, That keeps on updating automatically
It will output the results as
answered Nov 10, 2017 at 5:55
best shortcode for this section:
answered Mar 15, 2019 at 16:48
1
You can use this in footer sections to get dynamic copyright year answered Sep 9, 2017 at 5:51
imtaherimtaher 4144 silver badges9 bronze badges
Pang 9,193146 gold badges85 silver badges118 bronze badges answered Apr 1, 2020 at 13:39
Hernán EcheHernán Eche 6,15711 gold badges51 silver badges73 bronze badges In Laravel
In PHP
answered Jun 16, 2021 at 9:50
in my case the copyright notice in the footer of a wordpress web site needed updating. thought simple, but involved a step or more thann anticipated.
this might not be the same case for many, however we've come across this pattern among quite a number of our client sites and thought it would be best to document here.
Dharman♦ 27.9k21 gold badges75 silver badges127 bronze badges answered Nov 20, 2020 at 21:11
allenskiallenski 1,4254 gold badges20 silver badges35 bronze badges 0 Print current month with M, day with D and year with Y.
answered Jan 28, 2021 at 6:28
BilalBilal 2,29021 silver badges42 bronze badges For more pricise in second param in date function strtotime return the timestamp passed by param
With datetime class
answered Mar 2, 2021 at 12:31
2 create a helper function and call it
answered Sep 8, 2021 at 13:34
How can I get current year in PHP?To get the current year using PHP's date function, you can pass in the “Y” format character like so: //Getting the current year using //PHP's date function. $year = date("Y"); echo $year; The example above will print out the full 4-digit representation of the current year.
How can get current year start and end date in PHP?$year = date('Y') - 1; // Get current year and subtract 1 $start = mktime(0, 0, 0, 1, 1, $year); $end = mktime(0, 0, 0, 12, 31, $year);
What does date () do in PHP?The date/time functions allow you to get the date and time from the server where your PHP script runs. You can then use the date/time functions to format the date and time in several ways. Note: These functions depend on the locale settings of your server.
How can I get current month in PHP?php $transdate = date('m-d-Y', time()); echo $transdate; $month = date('m', strtotime($transdate)); if ($month == "12") { echo "
December is the month :)"; } else { echo " The month is probably not December"; } ?> |