php get current month
How can i get the current month and previous three months using PHP
Will any one tell me how to get the current month and previous three months using PHP
output will be: 09:Oct:20
9 Answers 9
for full textual representation of month you need to pass «F»:
for previous month you can use
echo date(«y:F:d»,strtotime(«-1 Months»)) ;
Watch out for the FUAH! The other answers will fail when performed on the 31st of the month. Use this instead:
For more, see my answer here
If you want to be OOP about it, try this:
Try using the built in strtotime function in PHP and using ‘F’ for full textual output:
You’ll need to use date(«F»); to get the full text representation of the date.
Not the answer you’re looking for? Browse other questions tagged php date or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
PHP Function: Date and Time Example
PHP Date & Time Function With Example. In this article, we will learn how to use PHP date and time functions. Learn more about PHP date() & PHP time related functions like php strtotime() etc.
PHP Date and Time Function with Examples
In this php date and time example tutorial, we would love to share with you many examples of php date and time functions like, php date to timestamp, php get date, php date format yyyy mm dd, php strtotime, php get month from date, php now, change date format in php, php get year from date, get only date from datetime in php, etc. You can see the examples below:
PHP Date() Function
The PHP date() function formats a timestamp to a more readable date and time.
Syntax
Parameters
Parameter | Description |
---|---|
format | Required. Specifies the format of the timestamp |
timestamp | Optional. Specifies a timestamp. Default is the current date and time |
Here are some characters that are commonly used for dates:
PHP Date Function Examples
1. php show current date
In this below example, we will use php date function for get the current date in PHP. The format is a current date is Y-m-d.
2. php get current year
If you want to get current of current year date. You can use the php date function like below:
3. PHP get current month
If you want to get current month from date. You can use the php date function like below.
4. PHP get current date and day name
If you want to get current date of day name. You can use the PHP date function like below:
5. PHP day name
If you want to current day name in PHP. You can use the php date function like below:
6. PHP current day,date,month name, year
7. php get month name from date
If you want to get month name from date. You also use php date() function like below:
8. php get last day of month from date
Use the below example for get last day of month from date in php:
9. php get last date of month from date
Use the below example for get date of month from date in php:
10. php get previous month of given date
if you want to get previous month of given date in. Use the below PHP date() function:
If you want to get previous month, previous two month, previous three month, four, five month etc from the given date you can easily minues from the given date, like above example.
11. php get next month of given date
if you want to get next month of given date in. Use the below PHP date() function:
If you want to get next month, next two month, next three month, four, five month etc from the given date you can easily minues from the given date, like above example.
12. php get last day and date of given month
php get last day and date from given month. you can use the below example:
13. php get first day of month from date
If you want to get first day of month from date in php. You can use the php date() function like this:
14. convert current date to timestamp in PHP
Use the below example for convert current date to a timestamp in PHP:
15. php get current month number
If you want to get the current month number. use the below example for that:
16. get day name from date in php
You want to get day name from date in php. so you can use the below example:
17. php get current day of week
If you want to get current day of week in php. You can use the below example for that. it will return numeric value.
18. get the day of the week from a date
you want to get the day of the week from a date. So you can follow the below example for that. it will return numeric value.
PHP Time() Function
The time() function is a built-in function in PHP which returns the current time measured in the number of seconds since the Unix Epoch.
The number of seconds can be converted to the current date using date() function in PHP. Return current time as Unix timestamp
So here is the situation. In my db table i have a column called expire_date, where i put a certain date for each row. So for example the expire date is 28-04-2012
11 Answers 11
First get the date in one month with php:
Then you can do a select:
And there you have an array with all the items who expire in less than one month. If you want exactly the ones that expire in 1 month:
I’m surprised this answer is missing here:
Maybe you should do that in SQL rather than PHP no?
If you want to do in in PHP, here is a cleaner way than @Expert want to be
Notice that I use = and not less than, because if you just compare that it’s less than one month, then you will be emailing the users every day during the last month.
You can make use of the ADDDATE() function in your MySQL Query.
If you have an index on your expire_date column, this can be done quite fast if you query for entries with a WHERE statement like:
(More about CURDATE() right here)
If you do that every day exactly once it will only select all people at max once and not send them the email twice since it only selects those that are exactly 1 month away from their expire date.
Of course this works without any additional care about the specific representation of dates if you use a DATE fieldtype in your MySQL table.
How do I use PHP to get the current year?
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.
How would I make the year update automatically with PHP 4 or PHP 5?
31 Answers 31
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?)
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.
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:
With PHP heading in a more object-oriented direction, I’m surprised nobody here has referenced the built-in DateTime class:
or one-liner with class member access on instantiation (php>=5.4):
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.
This one gives you the local time:
And this one UTC:
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)
For 4 digit representation:
2 digit representation:
echo date(‘Y’) gives you current year, and this will update automatically since date() give us the current date.
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.
This code should do
And ‘echo’ this value.
If your server supports Short Tags, or you use PHP 5.4, you can use:
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 you can use it with one line
If you wanna increase or decrease the year another method; add modify line like below.
use a PHP date() function.
and the format is just going to be Y. Capital Y is going to be a four digit year.
Get full Year used:
Or get only two digit of year used like this:
My way to show the copyright, That keeps on updating automatically
It will output the results as
best shortcode for this section:
You can use this in footer sections to get dynamic copyright year
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.
Open footer.php in your theme’s folder.
Locate copyright text, expected this to be all hard coded but found:
Now we know the year is written somewhere in WordPress admin so locate that to delete the year written text. In WP-Admin, go to Options on the left main admin menu:
Then on next page go to the tab Disclaimers :
and near the top you will find Copyright year:
DELETE the © symbol + year + the empty space following the year, then save your page with Update button at top-right of page.
With text version of year now delete, we can go and add our year that updates automatically with PHP. Go back to chunk of code in STEP 2 found in footer.php and update that to this:
Done! Just need to test to ensure changes have taken effect as expected.
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.