New date function in php

PHP Date/Time Functions

The date/time functions allow you to get the date and time from the server where your PHP script runs. You can then use the date/time functions to format the date and time in several ways.

Note: These functions depend on the locale settings of your server. Remember to take daylight saving time and leap years into consideration when working with these functions.

Installation

The PHP date/time functions are part of the PHP core. No installation is required to use these functions.

Runtime Configuration

The behavior of these functions is affected by settings in php.ini:

Name Description Default PHP Version
date.timezone The default timezone (used by all date/time functions) «» PHP 5.1
date.default_latitude The default latitude (used by date_sunrise() and date_sunset()) «31.7667» PHP 5.0
date.default_longitude The default longitude (used by date_sunrise() and date_sunset()) «35.2333» PHP 5.0
date.sunrise_zenith The default sunrise zenith (used by date_sunrise() and date_sunset()) «90.83» PHP 5.0
date.sunset_zenith The default sunset zenith (used by date_sunrise() and date_sunset()) «90.83» PHP 5.0

PHP Date/Time Functions

Function Description
checkdate() Validates a Gregorian date
date_add() Adds days, months, years, hours, minutes, and seconds to a date
date_create_from_format() Returns a new DateTime object formatted according to a specified format
date_create() Returns a new DateTime object
date_date_set() Sets a new date
date_default_timezone_get() Returns the default timezone used by all date/time functions
date_default_timezone_set() Sets the default timezone used by all date/time functions
date_diff() Returns the difference between two dates
date_format() Returns a date formatted according to a specified format
date_get_last_errors() Returns the warnings/errors found in a date string
date_interval_create_from_date_string() Sets up a DateInterval from the relative parts of the string
date_interval_format() Formats the interval
date_isodate_set() Sets the ISO date
date_modify() Modifies the timestamp
date_offset_get() Returns the timezone offset
date_parse_from_format() Returns an associative array with detailed info about a specified date, according to a specified format
date_parse() Returns an associative array with detailed info about a specified date
date_sub() Subtracts days, months, years, hours, minutes, and seconds from a date
date_sun_info() Returns an array containing info about sunset/sunrise and twilight begin/end, for a specified day and location
date_sunrise() Returns the sunrise time for a specified day and location
date_sunset() Returns the sunset time for a specified day and location
date_time_set() Sets the time
date_timestamp_get() Returns the Unix timestamp
date_timestamp_set() Sets the date and time based on a Unix timestamp
date_timezone_get() Returns the time zone of the given DateTime object
date_timezone_set() Sets the time zone for the DateTime object
date() Formats a local date and time
getdate() Returns date/time information of a timestamp or the current local date/time
gettimeofday() Returns the current time
gmdate() Formats a GMT/UTC date and time
gmmktime() Returns the Unix timestamp for a GMT date
gmstrftime() Formats a GMT/UTC date and time according to locale settings
idate() Formats a local time/date as integer
localtime() Returns the local time
microtime() Returns the current Unix timestamp with microseconds
mktime() Returns the Unix timestamp for a date
strftime() Formats a local time and/or date according to locale settings
strptime() Parses a time/date generated with strftime()
strtotime() Parses an English textual datetime into a Unix timestamp
time() Returns the current time as a Unix timestamp
timezone_abbreviations_list() Returns an associative array containing dst, offset, and the timezone name
timezone_identifiers_list() Returns an indexed array with all timezone identifiers
timezone_location_get() Returns location information for a specified timezone
timezone_name_from_ abbr() Returns the timezone name from abbreviation
timezone_name_get() Returns the name of the timezone
timezone_offset_get() Returns the timezone offset from GMT
timezone_open() Creates new DateTimeZone object
timezone_transitions_get() Returns all transitions for the timezone
timezone_version_get() Returns the version of the timezonedb
Читайте также:  Php get all variable names

PHP Predefined Date/Time Constants

Constant Description
DATE_ATOM Atom (example: 2019-01-18T14:13:03+00:00)
DATE_COOKIE HTTP Cookies (example: Fri, 18 Jan 2019 14:13:03 UTC)
DATE_ISO8601 ISO-8601 (example: 2019-01-18T14:13:03+0000)
DATE_RFC822 RFC 822 (example: Fri, 18 Jan 2019 14:13:03 +0000)
DATE_RFC850 RFC 850 (example: Friday, 18-Jan-19 14:13:03 UTC)
DATE_RFC1036 RFC 1036 (example: Friday, 18-Jan-19 14:13:03 +0000)
DATE_RFC1123 RFC 1123 (example: Fri, 18 Jan 2019 14:13:03 +0000)
DATE_RFC2822 RFC 2822 (example: Fri, 18 Jan 2019 14:13:03 +0000)
DATE_RFC3339 Same as DATE_ATOM (since PHP 5.1.3)
DATE_RFC3339_EXTENDED RFC3339 Extended format (since PHP 7.0.0) (example: 2019-01-18T16:34:01.000+00:00)
DATE_RSS RSS (Fri, 18 Jan 2019 14:13:03 +0000)
DATE_W3C World Wide Web Consortium (example: 2019-01-18T14:13:03+00:00)
SUNFUNCS_RET_TIMESTAMP Timestamp (since PHP 5.1.2)
SUNFUNCS_RET_STRING Hours:minutes (example: 09:41) (since PHP 5.1.2)
SUNFUNCS_RET_DOUBLE Hours as a floating point number (example: 9.75) (since PHP 5.1.2)

Источник

date_create

This is the procedural version of DateTime::__construct() .

Unlike the DateTime constructor, it will return false instead of an exception if the passed in datetime string is invalid.

Parameters

Return Values

Returns a new DateTime instance. Procedural style returns false on failure.

See Also

  • DateTimeImmutable::__construct() — Returns new DateTimeImmutable object
  • DateTimeImmutable::createFromFormat() — Parses a time string according to a specified format
  • DateTime::__construct() — Returns new DateTime object

User Contributed Notes 1 note

Notice php by default assume the give string as such format:
‘-‘ is ‘y-m-d’
‘/’ is ‘m/d/y’

Unless the given string has Y or M,
that is year is written as full year ‘2019’, or month is written as English shorthand ‘Jan’,
the default assumption will be applied, where the date might be incorrect.

The following code show a quick test: (true as of php 7.2)
$date = [
‘2019-1-3’,
’19-1-3′,
‘3-1-2019’,
‘3-Jan-19’,
‘3-1-19’, //php assume as y-m-d not d-m-y

‘2019/3/1’,
’19/3/1′, //fail, php think is month 19
‘1/3/2019’, //php think is m/d/y
‘1/3/19’
];

//Y-M-d
foreach($date as $i => $d) echo $i .»\r\n»;
var_dump(date_format(date_create($d), ‘Y-M-d’));
echo «\r\n»;
>

  • Date/Time Functions
    • checkdate
    • date_​add
    • date_​create_​from_​format
    • date_​create_​immutable_​from_​format
    • date_​create_​immutable
    • date_​create
    • date_​date_​set
    • date_​default_​timezone_​get
    • date_​default_​timezone_​set
    • date_​diff
    • date_​format
    • date_​get_​last_​errors
    • date_​interval_​create_​from_​date_​string
    • date_​interval_​format
    • date_​isodate_​set
    • date_​modify
    • date_​offset_​get
    • date_​parse_​from_​format
    • date_​parse
    • date_​sub
    • date_​sun_​info
    • date_​sunrise
    • date_​sunset
    • date_​time_​set
    • date_​timestamp_​get
    • date_​timestamp_​set
    • date_​timezone_​get
    • date_​timezone_​set
    • date
    • getdate
    • gettimeofday
    • gmdate
    • gmmktime
    • gmstrftime
    • idate
    • localtime
    • microtime
    • mktime
    • strftime
    • strptime
    • strtotime
    • time
    • timezone_​abbreviations_​list
    • timezone_​identifiers_​list
    • timezone_​location_​get
    • timezone_​name_​from_​abbr
    • timezone_​name_​get
    • timezone_​offset_​get
    • timezone_​open
    • timezone_​transitions_​get
    • timezone_​version_​get

    Источник

    PHP Date and Time

    The PHP date() function is used to format a date and/or a time.

    The PHP Date() Function

    The PHP date() function formats a timestamp to a more readable date and time.

    Syntax

    Parameter Description
    format Required. Specifies the format of the timestamp
    timestamp Optional. Specifies a timestamp. Default is the current date and time

    A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.

    Get a Date

    The required format parameter of the date() function specifies how to format the date (or time).

    Here are some characters that are commonly used for dates:

    • d — Represents the day of the month (01 to 31)
    • m — Represents a month (01 to 12)
    • Y — Represents a year (in four digits)
    • l (lowercase ‘L’) — Represents the day of the week

    Other characters, like»/», «.», or «-» can also be inserted between the characters to add additional formatting.

    The example below formats today’s date in three different ways:

    Example

    echo «Today is » . date(«Y/m/d») . «
    «;
    echo «Today is » . date(«Y.m.d») . «
    «;
    echo «Today is » . date(«Y-m-d») . «
    «;
    echo «Today is » . date(«l»);
    ?>

    Use the date() function to automatically update the copyright year on your website:

    Example

    Get a Time

    Here are some characters that are commonly used for times:

    • H — 24-hour format of an hour (00 to 23)
    • h — 12-hour format of an hour with leading zeros (01 to 12)
    • i — Minutes with leading zeros (00 to 59)
    • s — Seconds with leading zeros (00 to 59)
    • a — Lowercase Ante meridiem and Post meridiem (am or pm)

    The example below outputs the current time in the specified format:

    Example

    Note that the PHP date() function will return the current date/time of the server!

    Get Your Time Zone

    If the time you got back from the code is not correct, it’s probably because your server is in another country or set up for a different timezone.

    So, if you need the time to be correct according to a specific location, you can set the timezone you want to use.

    The example below sets the timezone to «America/New_York», then outputs the current time in the specified format:

    Example

    Create a Date With mktime()

    The optional timestamp parameter in the date() function specifies a timestamp. If omitted, the current date and time will be used (as in the examples above).

    The PHP mktime() function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

    Syntax

    The example below creates a date and time with the date() function from a number of parameters in the mktime() function:

    Example

    Create a Date From a String With strtotime()

    The PHP strtotime() function is used to convert a human readable date string into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

    Syntax

    The example below creates a date and time from the strtotime() function:

    Example

    PHP is quite clever about converting a string to a date, so you can put in various values:

    Example

    $d=strtotime(«next Saturday»);
    echo date(«Y-m-d h:i:sa», $d) . «
    «;

    However, strtotime() is not perfect, so remember to check the strings you put in there.

    More Date Examples

    The example below outputs the dates for the next six Saturdays:

    Example

    $startdate = strtotime(«Saturday»);
    $enddate = strtotime(«+6 weeks», $startdate);

    while ($startdate < $enddate) echo date("M d", $startdate) . "
    «;
    $startdate = strtotime(«+1 week», $startdate);
    >
    ?>

    The example below outputs the number of days until 4th of July:

    Example

    $d1=strtotime(«July 04»);
    $d2=ceil(($d1-time())/60/60/24);
    echo «There are » . $d2 .» days until 4th of July.»;
    ?>

    Complete PHP Date Reference

    For a complete reference of all date functions, go to our complete PHP Date Reference.

    The reference contains a brief description, and examples of use, for each function!

    Источник

Оцените статью