SQL PHP HTML ASP JavaScript articles and free scripts to download If you are facing any problem in viewing this page, please tell us
 
 

Finding month, weekday, day of the year for any date value


From a future date, past date or present date we can get different details like month name , weekday, month number, weekday number, day of the month etc by using getdate() function in PHP. This function takes time stamp of the required date as input and returns an array of details for that date.

By taking time stamp as input we can also use date() function to get the month name, day of the week etc of any date value. This we will discuss at the second part of this tutorial. Now let us discuss on getting date details of any future or past using getdate() function.

Here is the syntax of getdate() function.

$d_var=getdate([timestamp]);


Here timestamp is he optional argument by which we have to give the timestamp of the required date. By default it takes the current date and time as time stamp. Getdate function returns an array with different elements listed here.

"mday" - day of the month
"wday" - day of the week, numeric (1 for Monday)
"mon" - month, numeric (08 for August)
"year" - year, numeric - like 2008
"yday" - day of the year, numeric; i.e. "256"
"weekday" - day of the week, textual, full; i.e. "Friday"
"month" - month, textual, full; i.e. "August"
“seconds”
“minutes"
"hours"


Let us take one example and see how to find out all the above values for a date

$d_var=getdate(mktime(0,0,0,8,15,2008));


echo "Month name in Full = $d_var[month]<br>";
echo "Month in numeric = $d_var[mon]<br>";

echo "Day of the Week, numeric = $d_var[wday]<br>";
echo "Day of the Week, Full name = $d_var[weekday]<br>";

echo "Day of the month = $d_var[mday]<br>";
echo "Day of the year = $d_var[yday]<br>";

echo "Day of the year = $d_var[year]<br>";


You can see how the above code is used to get different values of a date. Here mktim() function is used to generate time stamp of the given date with hour, minute and second values and then the same time stamp is used as input to getdate function to get the date array.

Getting the name of month, year, day, weekday etc by using date function

After getting the timestamp of the required date with time. We can use date function to get all details of the particular date.

Here is the code

$tm=mktime(0,0,0,8,15,2008);
echo date("M",$tm);


The above code will print Aug as month name for the given date value. We can use different date formats inside the date function to get desired date part value as per requirement.

Click here to get similar function in ASP to get date parts


Further readings
PHP Date & Time functions
PHP Date Time
Validating Date: Checking if date exist
PHP Date Today
PHP yesterday and previous 7 days from today
PHP Date Format
Time stamp generator
Date selection drop down list box
mktime function to generate time stamp
Difference in Two dates by using mktime function
Finding month, weekday, day of the year etc for any date value
Breaking the date string to get month date and year using array















Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
PHP Tutorials
Date & Time
PHP Monthly Planner
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.