Date Function

1.date : The PHP function formats a timestamp to a more readable date and time. The required format parameter in the date() function specifies how to format the date/time.
Syntax: date(“date formate”)
Date formate:DAYS
d - day of the month 2 digits (01-31) j - day of the month (1-31)
D - 3 letter day (Mon - Sun)
l - full name of day (Monday - Sunday) N - 1=Monday, 2=Tuesday, etc (1-7)
S - suffix for date (st, nd, rd)
w - 0=Sunday, 1=Monday (0-6) z - day of the year (1=365)            MONTH
F - Full name of month (January - December) m - 2 digit month number (01-12)
n - month number (1-12)
M - 3 letter month (Jan - Dec) t - Days in the month (28-31)

WEEK
W - week of the year (1-52)
YEAR      TIME
L - leap year (0 no, 1 yes)              a - am or pm
o - ISO-8601 year number (Ex. 1979, 2006)            A - AM or PM
Y - four digit year (Ex. 1979, 2006)             B - Swatch Internet time (000 - 999)
y - two digit year (Ex. 79, 06)       g - 12 hour (1-12)
                G - 24 hour c (0-23)
                h - 2 digit 12 hour (01-12)
                H - 2 digit 24 hour (00-23)
                i - 2 digit minutes (00-59)
                s 0 2 digit seconds (00-59)

2.  getdate : The function returns date/time information of a timestamp or the current local date/time.
Syntax: array getdate()
<?php
print_r(getdate());
?>
Output: //Array ( [seconds] => 57 [minutes] => 17 [hours] => 13 [mday] => 3 [wday] => 3 [mon] => 7 [year] => 2013 [yday] => 183 [weekday] => Wednesday [month] => July [0] => 1372850277 )
3. setdate : The function is used to set the user define date it takes three arguments.
Syntax: void setdate(integer year,integer month, integer day)

<?php
$date = new DateTime();
$date->setDate(2001, 2, 3);
echo $date->format('Y-m-d'); //2001-02-03
?>

3.   setdate : The function is used to set the user define date it takes three arguments.
Syntax: void setdate(integer year,integer month, integer day)

<?php
$date = new DateTime();
$date->setDate(2001, 2, 3);
echo $date->format('Y-m-d'); //2001-02-03
?>




4.  checkdate : The checkdate() function is used to validate a date.
Syntax: bool checkdate(integer month, integer day, integer year)

<?php
if(checkdate(03,20,2010))            //true because it is valid date echo "true";
else
echo "false";
?>
5. time : The time() function returns the current time in the number of seconds since the Unix Epoch (January1 1970 00:00:00 GMT).
Syntax: datetime time()

<?php
$t=time();
echo($t . "<br>");            //1372852042 echo(date("Y-m-d",$t)); //2013-07-03
?>
6.   mktime : The mktime() function returns the Unix timestamp for a date.
Syntax: mktime(hour,minute,second,month,day,year,is_dst);

<?php
echo date("d:m:y",mktime(0,0,0,10,3,1975)); //03:10:75
?>
7.  date_add : The date_add() function adds some days, months, years, hours, minutes, and seconds to a date.
Syntax: date_add(object,interval);
<?php
$date=date_create("2013-03-15");
date_add($date,date_interval_create_from_date_string("40 days"));            // 2013-04-24 echo date_format($date,"Y-m-d");
?>
8. date_create : The function Return a new DateTime object, and then format the date.
Syntax: date_create(time,timezone);
9.  date_format : The date_format() function returns a date formatted according to the specified format.
Syntax: date_format(object,format);
<?php
$date=date_create("2013-03-15");
echo date_format($date,"Y/m/d H:i:s");               // 2013/03/15 00:00:00
?>
10.   gmdate : The function formats a GMT/UTC date and time, and returns the formatted date string.
Syntax: gmdate(format,timestamp);

<?php
echo gmdate("l") . "<br>";// Wednesday
echo gmdate("l jS \of F Y h:i:s A"); // Wednesday 3rd of July 2013 05:52:40 PM
?>
11.  localtime : The function returns the local time. Returns an array that contains the components of a Unix timestamp.
Syntax: localtime();
          <?php print_r(localtime());
       ?>
Output:
Array ( [0] => 58 [1] => 57 [2] => 19 [3] => 3 [4] => 6 [5] => 113 [6] => 3 [7] => 183 [8] => 1 )
12. strftime : The strftime() function formats a local time and/or date according to locale settings.
Syntax: gmdate(format,timestamp);
           

<?php
echo(strftime("%Y. %B %d. %A. %X %Z"));
?>
Output: 2013. July 03. Wednesday. 8:02:30 PM India Standard Time

format
·     %a - abbreviated weekday name
·     %A - full weekday name
·     %b - abbreviated month name
·     %B - full month name
·     %c - preferred date and time representation
·     %C - century number (the year divided by 100, range 00 to 99)
·     %d - day of the month (01 to 31)
·     %D - same as %m/%d/%y
·     %e - day of the month (1 to 31)
·     %g - like %G, but without the century
·     %G - 4-digit year corresponding to the ISO week number (see %V).
·     %h - same as %b
·     %H - hour, using a 24-hour clock (00 to 23)
·     %I - hour, using a 12-hour clock (01 to 12)
·     %j - day of the year (001 to 366)
·     %m - month (01 to 12)
·     %M - minute
·     %n - newline character
·     %p - either am or pm according to the given time value
·     %r - time in a.m. and p.m. notation
·     %R - time in 24 hour notation
·     %S - second
·     %t - tab character
·     %T - current time, equal to %H:%M:%S
·     %u - weekday as a number (1 to 7), Monday=1. Warning: In Sun Solaris Sunday=1
·     %U - week number of the current year, starting with the first Sunday as the first day of the first week
·     %V - The ISO 8601 week number of the current year (01 to 53), where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week
·     %W - week number of the current year, starting with the first Monday as the first day of the first week
·     %w - day of the week as a decimal, Sunday=0
·     %x - preferred date representation without the time
·     %X - preferred time representation without the date
·     %y - year without a century (range 00 to 99)
·     %Y - year including the century
·     %Z or %z - time zone or name or abbreviation
·     %% - a literal % character

13. strptime : The strptime() function parses a time/date generated with strftime().
Syntax: strptime(date,format); //support in after php5.1 version
<?php
$format="%d/%m/%Y %H:%M:%S";
$strf=strftime($format);
 echo("$strf"); print_r(strptime($strf,$format));
?>
14. strtotime : The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

Note: Be aware of dates in the m/d/y or d-m-y formats; if the separator is a slash (/), then the American m/d/y is assumed. If the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential errors, you should YYYY-MM-DD dates or date_create_from_format() when possible.
   <?php
echo(strtotime("now")."<br>");
echo(strtotime("3 October 2005")."<br>");
echo(strtotime("+5 hours")."<br>");
echo(strtotime("+1 week")."<br>");
echo(strtotime("+1 week 3 days 7 hours 5 seconds")."<br>");
echo(strtotime("next Monday")."<br>");
echo(strtotime("last Sunday"));
?>
Syntax: strtotime(time);
Output: it will in seconds from 1-1-1970

15.  gettimeofday : The gettimeofday() function returns the current time.
Syntax: array gettimeofday();
<?php
print_r(getdayoftime());
?>
Output:
Array ( [sec] => 1372875405 [usec] => 447070 [minuteswest] => -120 [dsttime] => 1 ) 1372875405.4635




No comments:

Post a Comment

=>Advanced Java Programming (J2EE)

1.  Syllabus 2. Unit Wise Question/Material 3. Paper 4. Previous Paper