Php date format date create

PHP date_create_from_format() Function

Return a new DateTime object formatted according to the specified format:

Definition and Usage

The date_create_from_format() function returns a new DateTime object formatted according to the specified format.

Syntax

Parameter Values

  • d — Day of the month; with leading zeros
  • j — Day of the month; without leading zeros
  • D — Day of the month (Mon — Sun)
  • l — Day of the month (Monday — Sunday)
  • S — English suffix for day of the month (st, nd, rd, th)
  • F — Monthname (January — December)
  • M — Monthname (Jan-Dec)
  • m — Month (01-12)
  • n — Month (1-12)
  • Y — Year (e.g 2013)
  • y — Year (e.g 13)
  • a and A — am or pm
  • g — 12 hour format without leading zeros
  • G — 24 hour format without leading zeros
  • h — 12 hour format with leading zeros
  • H — 24 hour format with leading zeros
  • i — Minutes with leading zeros
  • s — Seconds with leading zeros
  • u — Microseconds (up to six digits)
  • e, O, P and T — Timezone identifier
  • U — Seconds since Unix Epoch
  • (space)
  • # — One of the following separation symbol: ;. /. -,(,)
  • ? — A random byte
  • * — Random bytes until next separator/digit
  • ! — Resets all fields to Unix Epoch
  • | — Resets all fields to Unix Epoch if they have not been parsed yet
  • + — If present, trailing data in the string will cause a warning, not an error

Technical Details

Источник

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.

Читайте также:  Python программа для кружка

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 format date create

    • PHP Tutorial
    • PHP | Introduction
    • PHP Full Form
    • How to set PHP development environment in windows ?
    • LAMP installation and important PHP configurations on Ubuntu
    • PHP | Coding Standards
    • PHP | Basic Syntax
    • PHP | Variables
    • PHP echo and print
    • PHP | Data Types
    • PHP | Strings
    • PHP | Constants
    • PHP | Magic Constants
    • PHP | Decision Making
    • PHP | Loops
    • PHP | Superglobals
    • PHP | Regular Expressions
    • PHP Examples
    • Describe PHP Include and Require
    • PHP | Basics of File Handling
    • PHP | fopen( ) (Function open file or URL)
    • PHP fread( ) Function
    • PHP fclose( ) Function
    • PHP | fwrite( ) Function
    • PHP | Uploading File
    • PHP Cookies
    • PHP | Sessions
    • PHP Filter and Filter Constant
    • Implementing callback in PHP
    • Exception Handling in PHP
    • PHP | Arrays
    • PHP array() Function
    • PHP | array_change_key_case() Function
    • PHP array_chunk() Function
    • PHP | array_column() Function
    • PHP array_combine() Function
    • PHP array_count_values() Function
    • PHP array_diff() function
    • PHP array_diff_assoc() Function
    • PHP array_diff_key() Function
    • PHP array_diff_uassoc() Function
    • PHP array_diff_ukey() Function
    • PHP array_fill() function
    • PHP array_fill_keys() Function
    • PHP Array Functions Complete Reference
    • PHP Date and Time
    • PHP | checkdate() Function
    • PHP | date_create(), date_format(), date_add() Functions
    • PHP | date_create_from_format() Function
    • PHP | date_date_set() Function
    • PHP | date_default_timezone_get() Function
    • PHP | date_default_timezone_set() Function
    • PHP | date_diff() Function
    • PHP | date_get_last_errors() Function
    • PHP | date_isodate_set() Function
    • PHP | date_modify() Function
    • PHP | date_offset_get() Function
    • PHP | date_parse_from_format() Function
    • PHP | date_parse() Function
    • PHP | date_sub() Function
    • PHP basename( ) Function
    • PHP chgrp( ) Function
    • PHP chmod( ) Function
    • PHP chown( ) Function
    • PHP copy( ) Function
    • PHP dirname( ) Function
    • PHP disk_free_space( ) Function
    • PHP disk_total_space( ) Function
    • PHP feof( ) Function
    • PHP fflush( ) Function
    • PHP | fgetc( ) Function
    • PHP | fgets( ) Function
    • PHP | fgetss( ) Function
    • PHP | file_exists( ) Function
    • PHP file_get_contents() Function
    • PHP Filesystem Functions Complete Reference
    • PHP | ftp_alloc() function
    • PHP | ftp_chdir() function
    • PHP | ftp_close() function
    • PHP | ftp_connect() function
    • PHP | ftp_delete() function
    • PHP | ftp_exec() function
    • PHP | ftp_get() function
    • PHP | ftp_put() function
    • PHP | ftp_get_option() function
    • PHP | ftp_login() function
    • PHP | ftp_mdtm() Function
    • PHP | ftp_mkdir() function
    • PHP | Ds\Map allocate() Function
    • PHP | Ds\Map apply() Function
    • PHP | Ds\Map capacity() Function
    • PHP | DS\Map clear() Function
    • PHP | Ds\Map __construct() Function
    • PHP Ds\Map copy() Function
    • PHP | Ds\Map count() Function
    • PHP | Ds\Map diff() Function
    • PHP | Ds\Map filter() Function
    • PHP | Ds\Map first() Function
    • PHP Ds\Map Functions Complete Reference
    • SplDoublyLinkedList in PHP
    • PHP SplDoublyLinkedList add() Function
    • PHP SplDoublyLinkedList bottom() Function
    • PHP SplDoublyLinkedList count() function
    • PHP SplFixedArray count() Function
    • PHP SplFixedArray current() Function
    • PHP SplFixedArray getSize() Function
    • PHP SplFixedArray key() Function
    • PHP SplObjectStorage addAll() Function
    • PHP SplObjectStorage attach() Function
    • PHP SplObjectStorage contains() Function
    • PHP SplObjectStorage count() Function
    • PHP SplQueue::__construct() Function
    • PHP SPL Data structures Complete Reference
    • PHP Tutorial
    • PHP | Introduction
    • PHP Full Form
    • How to set PHP development environment in windows ?
    • LAMP installation and important PHP configurations on Ubuntu
    • PHP | Coding Standards
    • PHP | Basic Syntax
    • PHP | Variables
    • PHP echo and print
    • PHP | Data Types
    • PHP | Strings
    • PHP | Constants
    • PHP | Magic Constants
    • PHP | Decision Making
    • PHP | Loops
    • PHP | Superglobals
    • PHP | Regular Expressions
    • PHP Examples
    • Describe PHP Include and Require
    • PHP | Basics of File Handling
    • PHP | fopen( ) (Function open file or URL)
    • PHP fread( ) Function
    • PHP fclose( ) Function
    • PHP | fwrite( ) Function
    • PHP | Uploading File
    • PHP Cookies
    • PHP | Sessions
    • PHP Filter and Filter Constant
    • Implementing callback in PHP
    • Exception Handling in PHP
    • PHP | Arrays
    • PHP array() Function
    • PHP | array_change_key_case() Function
    • PHP array_chunk() Function
    • PHP | array_column() Function
    • PHP array_combine() Function
    • PHP array_count_values() Function
    • PHP array_diff() function
    • PHP array_diff_assoc() Function
    • PHP array_diff_key() Function
    • PHP array_diff_uassoc() Function
    • PHP array_diff_ukey() Function
    • PHP array_fill() function
    • PHP array_fill_keys() Function
    • PHP Array Functions Complete Reference
    • PHP Date and Time
    • PHP | checkdate() Function
    • PHP | date_create(), date_format(), date_add() Functions
    • PHP | date_create_from_format() Function
    • PHP | date_date_set() Function
    • PHP | date_default_timezone_get() Function
    • PHP | date_default_timezone_set() Function
    • PHP | date_diff() Function
    • PHP | date_get_last_errors() Function
    • PHP | date_isodate_set() Function
    • PHP | date_modify() Function
    • PHP | date_offset_get() Function
    • PHP | date_parse_from_format() Function
    • PHP | date_parse() Function
    • PHP | date_sub() Function
    • PHP basename( ) Function
    • PHP chgrp( ) Function
    • PHP chmod( ) Function
    • PHP chown( ) Function
    • PHP copy( ) Function
    • PHP dirname( ) Function
    • PHP disk_free_space( ) Function
    • PHP disk_total_space( ) Function
    • PHP feof( ) Function
    • PHP fflush( ) Function
    • PHP | fgetc( ) Function
    • PHP | fgets( ) Function
    • PHP | fgetss( ) Function
    • PHP | file_exists( ) Function
    • PHP file_get_contents() Function
    • PHP Filesystem Functions Complete Reference
    • PHP | ftp_alloc() function
    • PHP | ftp_chdir() function
    • PHP | ftp_close() function
    • PHP | ftp_connect() function
    • PHP | ftp_delete() function
    • PHP | ftp_exec() function
    • PHP | ftp_get() function
    • PHP | ftp_put() function
    • PHP | ftp_get_option() function
    • PHP | ftp_login() function
    • PHP | ftp_mdtm() Function
    • PHP | ftp_mkdir() function
    • PHP | Ds\Map allocate() Function
    • PHP | Ds\Map apply() Function
    • PHP | Ds\Map capacity() Function
    • PHP | DS\Map clear() Function
    • PHP | Ds\Map __construct() Function
    • PHP Ds\Map copy() Function
    • PHP | Ds\Map count() Function
    • PHP | Ds\Map diff() Function
    • PHP | Ds\Map filter() Function
    • PHP | Ds\Map first() Function
    • PHP Ds\Map Functions Complete Reference
    • SplDoublyLinkedList in PHP
    • PHP SplDoublyLinkedList add() Function
    • PHP SplDoublyLinkedList bottom() Function
    • PHP SplDoublyLinkedList count() function
    • PHP SplFixedArray count() Function
    • PHP SplFixedArray current() Function
    • PHP SplFixedArray getSize() Function
    • PHP SplFixedArray key() Function
    • PHP SplObjectStorage addAll() Function
    • PHP SplObjectStorage attach() Function
    • PHP SplObjectStorage contains() Function
    • PHP SplObjectStorage count() Function
    • PHP SplQueue::__construct() Function
    • PHP SPL Data structures Complete Reference

    Источник

    PHP date_create() Function

    Return a new DateTime object, and then format the date:

    More examples at the bottom of this page.

    Definition and Usage

    The date_create() function returns a new DateTime object.

    Syntax

    Parameter Values

    Parameter Description
    time Optional. Specifies a date/time string. NULL indicates the current time
    timezone Optional. Specifies the timezone of time. Default is the current timezone.

    Technical Details

    Return Value: Returns a new DateTime object on success. FALSE/Exception on failure
    PHP Version: 5.2+
    PHP Changelog: PHP 7.1: Microseconds is now filled with actual value, not just «00000».
    PHP 5.3: An exception is thrown if time is an invalid value. Previously an error was thrown

    More Examples

    Example

    Return a new DateTime object (with a given timezone), and then format the date and time:

    $date=date_create(«2013-03-15 23:40:00»,timezone_open(«Europe/Oslo»));
    echo date_format($date,»Y/m/d H:iP»);
    ?>

    Unlock Full Access 50% off

    COLOR PICKER

    colorpicker

    Join our Bootcamp!

    Report Error

    If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

    Thank You For Helping Us!

    Your message has been sent to W3Schools.

    Top Tutorials
    Top References
    Top Examples
    Get Certified

    W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

    Источник

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