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.toPrecision(6) //returns 123.450 (padding) anumber.toPrecision(4) //returns 123.5 (round up) anumber.toPrecision(2) //returns 1.2e+2 (you figure it out!) Round to a certain number of places For rounding decimals y

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

Html tags

HTML Basic Document <html> <head> <title>Title of document goes here</title> </head> <body> Visible text goes here... </body> </html> Heading Elements <h1>Largest Heading</h1> <h2> . . . </h2> <h3> . . . </h3> <h4> . . . </h4> <h5> . . . </h5> <h6>Smallest Heading</h6> Text Elements <p>This is a paragraph</p> <br /> (line break) <hr /> (horizontal rule) <pre>This text is preformatted</pre> Logical Styles <em>This text is emphasized</em> <strong>This text is strong</strong> <code>This is some computer code</code> Physical Styles <b>This text is bold</b> <i>This text is italic</i> Links Ordinary link: <a href="http://www.example.com/">Link-text goes here</a> Image-link: <a href="http://ww