Skip to main content

Date formats in php

Sometimes we need to display date in our comment , post , news etc.
For that as you aware we need to store that date in our database , the format of date in database is Y M D.

But that might be possible in some cases we need to display the date in different format.
One simple way for that is explode the date which is come from database by "-" and set according to your requirement.

But what happen when we want to display date in different format like
1 jan 2011
01 january  2011
01 january 11 etc...

The solution is  strtotime .

To display simple date in php

echo date("Y-m-d");

but when date comes from database at that time use :

date("j M Y ", strtotime( Here date comes form database  ));

j M Y is different types of format ..More formats are display below ::

Day

  • d: Day of the month with leading zeroes. Values are 01 through 31.
  • j: Day of the month without leading zeroes. Values 1 through 31
  • D: Day of the week abbreviations. Sun through Sat
  • l: Day of the week. Values Sunday through Saturday
  • w: Day of the week without leading zeroes. Values 0 through 6.
  • z: Day of the year without leading zeroes. Values 0 through 365.
 Month
    
  • m: Month number with leading zeroes. Values 01 through 12
  • n: Month number without leading zeroes. Values 1 through 12
  • M: Abbreviation for the month. Values Jan through Dec
  • F: Normal month representation. Values January through December.
  • t: The number of days in the month. Values 28 through 31.

 Year


  • L: 1 if it's a leap year and 0 if it isn't.
  • Y: A four digit year format
  • y: A two digit year format. Values 00 through 99.


Comments

Popular posts from this blog

Number Format In Javascript

Number.toFixed() :                                 Formats any number for "x" number of trailing decimals. The number is rounded up, and "0"s are used after the decimal point if needed to create the desired decimal length.   var profits=2489.8237 profits.toFixed(3) //returns 2489.824 (round up) profits.toFixed(2) //returns 2489.82 profits.toFixed(7) //returns 2489.8237000 (padding)   Number.toPrecision() :                                   Formats any number so it is of "x" length. Also called significant digits. A decimal point and "0"s are used if needed to create the desired length.   var anumber=123.45 anumber.toPreci...

Use of indexOf and charAt in javascript

indexOf(): The indexOf() method returns the position of the first occurrence of a specified value in a string. This method returns -1 if the value to search for never occurs. example :  <script type="text/javascript"> var str="Hello world!"; document.write(str.indexOf("d") + "<br />"); document.write(str.indexOf("WORLD") + "<br />"); document.write(str.indexOf("world")); </script> Output : 10 -1 6 charAt() The charAt() method returns the character at the specified index in a string. The index of the first character is 0, and the index of the last character in a string called "txt", is txt.length-1 <script type="text/javascript"> var str = "Hello world!"; document.write("First character: " + str.charAt(0) + "<br />"); document.write("Last character: " + str.charAt(str.l...

Check javascript is enable or disable

Hello there In our genreal practice of developing a webpage we use JAVASCRIPT. But do you know what will happen when in clients' browser javascript is disable and in your webpage you used it ?? Nothing will happen as per your expectation. It is good programming practice to check JAVASCRIPT is disabled or not . For example  <noscript>   <meta http-equiv = "refresh" content = "1;url=error.html" > </noscript>   This will redirect to an error page if script is disabled. Just replace error.html with the url of your error page. Or you also can do this   <noscript> <blink>Please enable javascript</blink> </noscript>