Notes To generate a timestamp from a string representation of the date, you may be able to use strtotime() .

Notes To generate a timestamp from a string representation of the date, you may be able to use strtotime() .

(current page) / Concentrate search box

(PHP Four, PHP Five, PHP 7)

date &mdash, Format a local time/date

Description

Comebacks a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time() .

Parameters

The format of the outputted date string . See the formatting options below. There are also several predefined date constants that may be used instead, so for example DATE_RSS contains the format string ',D, d M Y H:i:s',.

Unrecognized characters in the format string will be printed as-is. The Z format will always come back 0 when using gmdate() .

Since this function only accepts integer timestamps the u format character is only useful when using the date_format() function with user based timestamps created with date_create() .

The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time() .

Comeback Values

Comes back a formatted date string. If a non-numeric value is used for timestamp , FALSE is returned and an E_WARNING level error is emitted.

Errors/Exceptions

Every call to a date/time function will generate a E_NOTICE if the time zone is not valid, and/or a E_STRICT or E_WARNING message if using the system settings or the TZ environment variable. See also date_default_timezone_set()

Changelog

Now issues the E_STRICT and E_NOTICE time zone errors.

Examples

// set the default timezone to use. Available since PHP Five.1

// Prints something like: Monday

echo date ( ‘l jS \of F Y h:i:s A’ ),

echo “July 1, 2000 is on a ” . date ( “l” , mktime ( 0 , 0 , 0 , 7 , 1 , 2000 )),

// prints something like: Wed, 25 Sep 2013 15:28:57 -0700

echo date ( DATE_RFC2822 ),

echo date ( DATE_ATOM , mktime ( 0 , 0 , 0 , 7 , 1 , 2000 )),

You can prevent a recognized character in the format string from being expanded by escaping it with a preceding backslash. If the character with a backslash is already a special sequence, you may need to also escape the backslash.

// prints something like: Wednesday the 15th

echo date ( ‘l \t\h\e jS’ ),

It is possible to use date() and mktime() together to find dates in the future or the past.

$tomorrow = mktime ( 0 , 0 , 0 , date ( “m” ) , date ( “d” )+ 1 , date ( “Y” )),

$lastmonth = mktime ( 0 , 0 , 0 , date ( “m” )- 1 , date ( “d” ), date ( “Y” )),

$nextyear = mktime ( 0 , 0 , 0 , date ( “m” ), date ( “d” ), date ( “Y” )+ 1 ),

This can be more reliable than simply adding or subtracting the number of seconds in a day or month to a timestamp because of daylight saving time.

Some examples of date() formatting. Note that you should escape any other characters, as any which presently have a special meaning will produce undesirable results, and other characters may be assigned meaning in future PHP versions. When escaping, be sure to use single quotes to prevent characters like \n from becoming newlines.

// Assuming today is March 10th, 2001, Five:16:Eighteen pm, and that we are in the

// Mountain Standard Time (MST) Time Zone

$today = date ( “m.d.y” ), // 03.Ten.01

$today = date ( “j, n, Y” ), // Ten, Three, 2001

$today = date ( “Ymd” ), // 20010310

$today = date ( ‘h-i-s, j-m-y, it is w Day’ ), // 05-16-18, 10-03-01, 1631 1618 6 Satpm01

$today = date ( ‘\i\t \i\s \t\h\e jS \d\a\y.’ ), // it is the 10th day.

$today = date ( “D M j G:i:s T Y” ), // Sat Mar Ten 17:16:Eighteen MST 2001

$today = date ( ‘H:m:s \m \i\s\ \m\o\n\t\h’ ), // 17:03:Eighteen m is month

$today = date ( “H:i:s” ), // 17:16:Legal

$today = date ( “Y-m-d H:i:s” ), // 2001-03-10 17:16:Eighteen (the MySQL DATETIME format)

To format dates in other languages, you should use the setlocale() and strftime() functions instead of date() .

Notes

To generate a timestamp from a string representation of the date, you may be able to use strtotime() . Additionally, some databases have functions to convert their date formats into timestamps (such as MySQL',s », UNIX_TIMESTAMP function).

Timestamp of the commence of the request is available in $_SERVER[‘REQUEST_TIME’] since PHP Five.1.

See Also

  • gmdate() – Format a GMT/UTC date/time
  • idate() – Format a local time/date as integer
  • getdate() – Get date/time information
  • getlastmod() – Gets time of last page modification
  • mktime() – Get Unix timestamp for a date
  • strftime() – Format a local time/date according to locale settings
  • time() – Come back current Unix timestamp
  • strtotime() – Parse about any English textual datetime description into a Unix timestamp
  • Predefined DateTime Constants

User Contributed Notes 27 notes

echo date ( “YW” , strtotime ( “2011-01-07” )), // gives 201101

echo date ( “YW” , strtotime ( “2011-12-31” )), // gives 201152

echo date ( “YW” , strtotime ( “2011-01-01” )), // gives 201152 too

echo date ( “oW” , strtotime ( “2011-01-07” )), // gives 201101

echo date ( “oW” , strtotime ( “2011-12-31” )), // gives 201152

echo date ( “oW” , strtotime ( “2011-01-01” )), // gives 201052 (Year is different than previous example)

Y is year from the date

o is ISO-8601 year number

W is ISO-8601 week number of year

if using ‘W’ for the week number use ‘o’ for the year.

// very first line of PHP

if( date_default_timezone_get ()!= $defaultTimeZone )) date_default_timezone_set ( $defaultTimeZone ),

function _date ( $format = “r” , $timestamp = false , $timezone = false )

$userTimezone = fresh DateTimeZone (!empty( $timezone ) ? $timezone : ‘GMT’ ),

$gmtTimezone = fresh DateTimeZone ( ‘GMT’ ),

$myDateTime = fresh DateTime (( $timestamp != false ? date ( “r” ,(int) $timestamp ): date ( “r” )), $gmtTimezone ),

$offset = $userTimezone ->, getOffset ( $myDateTime ),

comeback date ( $format , ( $timestamp != false ?(int) $timestamp : $myDateTime ->, format ( ‘U’ )) + $offset ),

echo ‘System Date/Time: ‘ . date ( “Y-m-d | h:i:sa” ). ‘<,br>,’ ,

echo ‘Fresh York Date/Time: ‘ . _date ( “Y-m-d | h:i:sa” , false , ‘America/New_York’ ). ‘<,br>,’ ,

echo ‘Belgrade Date/Time: ‘ . _date ( “Y-m-d | h:i:sa” , false , ‘Europe/Belgrade’ ). ‘<,br>,’ ,

echo ‘Belgrade Date/Time: ‘ . _date ( “Y-m-d | h:i:sa” , 514640700 , ‘Europe/Belgrade’ ). ‘<,br>,’ ,

This is the best and fastest solution for this problem. Working almost identical to date() function only as a supplement has the time zone option.

y a las 5pm del Lunes cambia a Martes.

echo “Today is Sun Two Oct 2016, day ” , date ( ‘w’ , strtotime ( ‘2016-10-02’ )), ” of this week. ” ,

echo “Day ” , date ( ‘w’ , strtotime ( ‘2016-10-02 Monday next week’ )), ” of next week is ” , date ( ‘d M Y’ , strtotime ( ‘2016-10-02 Monday next week’ )), “<,br />,” ,

echo “Day ” , date ( ‘w’ , strtotime ( ‘2016-10-02 Monday next week’ )), ” of next week is ” , date ( ‘d M Y’ , strtotime ( ‘2016-10-02 Monday next week’ )),

Today is Sun Two Oct 2016, day 7 of this week. Day 1 of next week is Ten Oct 2016

Today is Sun Two Oct 2016, day 7 of this week. Day 1 of next week is 03 Oct 2016

echo date ( ‘Y-m-d\TH:i:sO’ ),

echo date ( DateTime :: ISO8601 ),

echo (int) ‘0x10’ , //0

echo intval ( ‘0x10’ ), //0

echo date ( ‘s’ , ‘0x10’ ), //gives 16

//however, no octal conversion:

echo date ( ‘s’ , ‘010’ ), //gives Ten

function get_time_string ( $seconds )

comeback date ( ‘H:i:s’ , strtotime ( “2000-01-01 + $seconds SECONDS” )),

$days = floor (( time () – strtotime ( “01-Jan-2006” ))/ 86400 ),

print( ” $days days have passed.\n” ),

$timestamp = time () – ( $ago * 86400 ),

$month = date ( “M” , mktime ( 0 , 0 , 0 , $m , 1 , 2000 )),

function is_leapyear_broken ( $year = 2004 ) <

come back ( $year % Four )== 0 ,

A function not using php’s date() function that will also account for this puny anomaly in leap years:

function is_leapyear_working ( $year = 2004 ) <

if((( $year % Four == 0 ) &,&, ( $year % 100 != 0 )) || $year % 400 == 0 ) <

function is_leapyear ( $year = 2004 ) <

$is_leap = date ( ‘L’ , strtotime ( ” $year -1-1″ )),

I switched the code a little to replicate the functionality of date_parse_from_format, once I don’t have PHP Five.Trio.0 yet. This might be useful for someone. Hope you don’t mind switching your code tcasparr at gmail dot com.

* Elementary function to take in a date format and comeback array of associated

* formats for each date element

* @param string $strFormat

* Example: Y/m/d g:i:s becomes

* This function is needed for PHP <, Five.Trio.0

function dateParseFromFormat ( $stFormat , $stData )

$aPieces = split ( ‘[:/.\ \-]’ , $stFormat ),

$aDatePart = split ( ‘[:/.\ \-]’ , $stData ),

foreach( $aPieces as $key =>, $chPiece )

$aDataRet [ ‘day’ ] = $aDatePart [ $key ],

$aDataRet [ ‘month’ ] = $aDatePart [ $key ],

$aDataRet [ ‘year’ ] = $aDatePart [ $key ],

$aDataRet [ ‘hour’ ] = $aDatePart [ $key ],

$aDataRet [ ‘minute’ ] = $aDatePart [ $key ],

$aDataRet [ ‘2nd’ ] = $aDatePart [ $key ],

function changeDateFormat ( $stDate , $stFormatFrom , $stFormatTo )

// When PHP Five.Trio.0 becomes available to me

//For now I use the function above

$date = dateParseFromFormat ( $stFormatFrom , $stDate ),

come back date ( $stFormatTo , mktime ( $date [ ‘hour’ ],

function getOrthodoxEaster ( $date )<

Takes any Gregorian date and comes back the Gregorian

date of Orthodox Easter for that year.

$year = date ( “Y” , $date ),

$rb = Two * $r2 + Four * $r3 + 6 * $r4 ,

//Orthodox Easter for this year will fall $rc days after April Trio

come back strtotime ( “Trio April $year + $rc days” ),

For example using a 24 hour notation without leading zeros is the option ‘%G’ in PHP but ‘%k’ in MySQL.

When using dynamically generated date formatting string, be careful to generate the correct options for either PHP or MySQL.

// If the commence and end dates are given in the wrong order, roll them.

comeback networkdays ( $e , $s , $holidays ),

$w = floor (( $e – $s )/( 86400 * 7 )), # Divide the difference in the two times by seven days to get the number of weeks.

if ( $ed >,= $sd ) < $w –, ># If the end date falls on the same day of the week or a later day of the week than the begin date, subtract a week.

$nwd = max ( 6 – $sd , 0 ), # If the embark day is Saturday or Sunday, add zero, otherewise add six minus the weekday number.

$nwd += min ( $ed , Five ), # If the end day is Saturday or Sunday, add five, otherwise add the weekday number.

$nwd += $w * Five , # Add five days for each week in inbetween.

foreach ( $holidays as $h ) <

$h = strtotime ( $h ),

$end = strtotime ( “13 December 2010” ),

$holidays [] = “Four July 2010” , // Falls on a Sunday, doesn’t affect count

$holidays [] = “6 September 2010” , // Falls on a Monday, reduces count by one

$j1 = mktime ( 0 , 0 , 0 , 1 , 1 , $y ),

if ( date ( “N” , $j1 ) == 6 )

elseif ( date ( “N” , $j1 ) == Five or date ( “N” , $j1 ) == 7 )

if ( date ( “N” , $j1 ) == 6 or date ( “N” , $j1 ) == 7 )

<,?php $mysqltime = date ( “Y-m-d H:i:s” , $phptime ), ?>,

$first_of_month_index = date ( ‘N’ , strtotime ( ‘Four/1/1990’ )) % 7 ,

* formats the date passed into format required by ‘datetime’ attribute of <,date>, tag

* if no intDate supplied, uses current date.

* @param intDate integer optional

function getDateTimeValue ( $intDate = null ) <

$strDate = $intDate ? date ( $strFormat , $intDate ) : date ( $strFormat ) ,

For those of us still needing it, here is plain hack to get the job done.

if ($date->,format(‘w’) === 0) // Sunday

come back (int)$date->,format(‘W’) – 1,

$timezone = fresh \ DateTimeZone ( $userTimeZone ),

$date = fresh \ DateTime ( ‘@’ . time (), $timezone ),

$date ->, setTimezone ( $timezone ),

$timezone = fresh \ DateTimeZone ( $userTimeZone ),

$date = fresh \ DateTime ( ‘@’ . time (), $timezone ),

$date ->, setTimezone ( $timezone ),

$now = $date ->, getTimestamp () + $date ->, getOffset (),

Related movie: Shemale Date Cam Online


Leave a Reply

Your email address will not be published. Required fields are marked *

*