date_create

(PHP 5 >= 5.2.0)

date_createAlias of DateTime::__construct()

Description

This function is an alias of: DateTime::__construct()

add a note add a note

User Contributed Notes 11 notes

up
12
Dok
10 years ago
If you want to create the DateTime object directly from a timestamp use this

<?php
$st
= 1170288000 //  a timestamp
$dt = new DateTime("@$st");
?>

See also: http://bugs.php.net/bug.php?id=40171
up
11
Marton Bodonyi
4 years ago
If you are getting an error like this:

Exception: DateTime::__construct(): Failed to parse time string (13/02/2013) at position 0 (1): Unexpected character in DateTime->__construct()

Note that when you create a new date object using a format with slashes and dashes (eg 02-02-2012 or 02/02/2012) it must be in the mm/dd/yy(yy) or mm-dd-yy(yy) format (rather than british format dd/mm/yy)! Months always before years (the american style) otherwise you'll get an incorrect date and may get an error like the one above (where PHP is crashing on trying to decode a 13th month).

Can catch you off guard because everything seems to be working fine and dandy until you hit a value over 12.
up
6
php at andysdrawings dot co dot uk
5 years ago
DateTime will recognise any number up to 12 as a [month], and any number up to 31 as a [day]; it calculates the resulting date to be [day] days after the start of [month].  This means that when a datetime object is created with more days than are found in that month, the date will be beyond the end of the monthe above (wher<?php (wher]; i new A3 otherwa (wher /><1'>(an are found e086he end of the monthe above (wh="refnd dasclass="default"><?php (wher]; i new A3 otherwa span><?php (whe,he end of the monthe (wher(r /><?p /><1-0er"3 00:00:00an are found ]; i new A3 otherwa span><?php (wher]; i new A3 otherwa (wher /><1'>(an are found e086he end of the monthe above (wh="refnd dasclass="default"><?php (wher]; i new A3 otherwa span><?php (whe,he end of the monthe (wher(r /><?p /><1-07-01 00:00:00an arelass="string">"@
Canef="http://bugs.php.net/bug.php?id=40171" rel="nofollow" target723blank">http://bugs.php.net/bug.php?id=4017723blaunction.date-create&vote=up" title="V723bclass="note" id="111286">
down723ble
mke this..."> 11
php at andys5rawi07t():39co dot uk<2em> &pa723blau
"?> pl="next" href="http://fr2.php.net/mane_date_set &raqu http://bugs.php.net/bug.php?id=40177459launction.date-create&vote=up" title="V7459class="note" id="111286"> ('se you are')n>an aree086h"3\n">an arephert_r(d ther< are')n>an aree086h"4\n">an arephert_r(d theran are[/e="2]class=an areph/dl>s="html] >5 yearef='/class=(an are found found [ <13 00:00:00.000000an are found found [y]; zone.net/] =lass 3an are found found [y]; zone] =lass Europe/LThionan arect(): F2t(): F3class=ong>5 yearef='/class=(an are found found [ <13 00:00:00.000000an are found found [y]; zone.net/] =lass 3an are found found [y]; zone] =lass Europe/LThionan arect(): F4an are dayn arlass>n are daynlassFafunat dd/ day/nlass: found Uncaughh exd="Hcom ' id="Hcom'resultich" na '1286">
If you are getting an error like this:

E's af/n th" ti:23an areStack
< class=#0f/n th" ti(23):ption: Date claTime::__constru'se you are')class=#1 { wid}an are found thrl/vs af daynlass/n th" ti day/nlasseric='/ma daynlass23 day/nlass dayn arlass>n are[/e="2]class=an areWhichumber up to o/mancharactend dashr /uunct yearsecodee>http://bugs.php.net/bug.php?id=4017374blaunction.date-create&vote=up" title="V374bclass="note" id="111286">
down374ble
11
&pa374blau
&l="1ual/alwayhtml:__constrew nd ption: Da found th].&a>es.net13th mid greg
ate index.t [mo/>
Can catch you off guard because everything seems to be wor4938 fine and dandy until you hit a value ove4938 function.date-create&vote=up" title="V4938class="note" id="108674">
Dok6em> &pa4938 fu
5 yeah/enhe o laxt [monh midfs.utiruef=' Ishrpp><?php (wher]; i new A3 otherwa (wher /><1'>(an are found ]; i new A3 otherwa span><?php (wher]; i new A3 otherwa (wher /><1'>(an arelass="string">"@n arelass="stn areWe a>

Can catch you off guard because everything seems to be w74599lank">http://bugs.php.net/bug.php?id=4074599lafunction.date-create&vote=up" title=4599iv class="note" id="76216">
down
12
Dok
&pa4599laf
in1): Unexpe namp
d="al/en/div> <:__constrew, wh/reftringan oo ipected chttolass ' Ishitiseco p;perm=ning>10 yould If you want found found $y]; Zone reate the DateTime object directly from a timestamp(wher(<?p +2 hoursu want found found e end of the monthe above (wh="refabove (_y]; zone.sesclass="default"><?php (wher]; i new A3 otherwa span>((<?php (wher]; i new A3 otherwa above (wh$="reSrc>((<?php (wher]; i new A3 otherwa (wher(<?php (wher]; i new A3 otherwa above (wh$="reSrc>(<?p t (rath! ="repcod14:50:00( ((
f the monthe above (whnd dasclass="default"><?php (wher]; i new A3 otherwa (wher(<?p INCORRECT! 1286"> "@$st[red.strir >"@imrt to " nua h 10te=onsul numat (rath,m-dd-12)seeeTimeesirbr b'/manur befo. Thayy]; zone p 04"al/en21086rgject= nud="al/en2/ n>doesn't p;pv86e TZ tionnexp id=]ss="phpcode">10 years agoonhe her thabetween oif> tyy]; zones,ecode avwi ago
If you want found found $y]; Zone reate the DateTime object directly from a timestamp(wher(<?p +2 hoursu want found found e end of the monthe above (wh$="reSrc reate the DateTime object directly from a timestamp(wher(<?php (wher]; i new A3 otherwa above (wh$="reSrc>((((
f the monthe above (whsespan Zone>((( ((
f the monthe above (whnd dasclass="default"><?php (wher]; i new A3 otherwa (wher(<?p CORRECT! 1286"> "@$stI'mua h suroningty nu nua bugnew eesirbr b'/manuur.
[red.stri>don't avwi agdago If you want found found $y]; Zone reate the DateTime object directly from a timestamp(wher(<?p +2 hoursu want found found e end of the monthe above (wh$="reSrc reate the DateTime object directly from a timestamp(wher(<?php (wher]; i new A3 otherwa above (wh$="reSrc>((
f the monthe above (whsespan Zone>((( ((
f the monthe above (whnd dasclass="default"><?php (wher]; i new A3 otherwa (wher(<?p CORRECT! 1286"> "@$st]>
Can catch you off guard because everything seems to be w85194lank">http://bugs.php.net/bug.php?id=4085194lafunction.date-create&vote=up" title85194iv class="note" id="76216">
wh="ref​gasi​ef=ti​
- actio hrefh moet/mane_dateactiolwher< ​ ​ ​ ​ - actio hrefh mo_set »r class= - actio soactiolass="clr class= - actiomo ifys="clr class= - actioofflas-gass="clr class= - actiospan e_date_set »r class= - actiospan raquor class= - actiosubraquor class= - actiosun-tionraquor class= - actiosunrin raquor class= - actiosunlass="clr class= - actioy]; olass="clr class= - actioy]; - actioy]; - actioy]; zone-gass="clr class= - actioy]; zone-lass="clr class= - actis="clr class= - gasactis="clr class=gasacti">gasacticldiv class -/="active"> - gasy]; ofdays="clr class=gasy]; ofday">gasy]; ofdaycldiv class -/="active"> - gmactis="clr class=gmacti">gmacticldiv class -/="active"> - gmmkTime Rel"r class=gmmkTime">gmmkTimecldiv class -/="active"> - gmstrfTime Rel"r class=gmstrfTime">gmstrfTimecldiv class -/="active"> - iactis="clr class=iacti">iacticldiv class -/="active"> - lo/maTime Rel"r class=lo/maTime">lo/maTimecldiv class -/="active"> - microTime Rel"r class=microTime">microTimecldiv class -/="active"> - mkTime Rel"r class=mkTime">mkTimecldiv class -/="active"> - strfTime Rel"r class=strfTime">strfTimecldiv class -/="active"> - strpTime Rel"r class=strpTime">strpTimecldiv class -/="active"> - strtoTime Rel"r class=strtoTimewhchttolasscldiv class -/="active"> - Time Rel"r class=Timewhlasscldiv class -/="active"> - Timezone-abbreviPHP&as4lisp Rel"r class=Timezone.​abbreviPHP&asi​eispwhlasszone.​abbreviPHP&asi​eispcldiv class -/="active"> - Timezone-id>&l=fiers4lisp Rel"r class=Timezone.​id>&l=fiersi​eispwhlasszone.​id>&l=fiersi​eispcldiv class -/="active"> - Timezone-lo/m"> -gass="clr class=lasszone.​lo/m"> .​gaswhlasszone.​lo/m"> .​gascldiv class -/="active"> - Timezone- e_dateabbrs="clr class=lasszone.​ ​ ​abbrwhlasszone.​ ​ ​abbrcldiv class -/="active"> - Timezone- egass="clr class=lasszone.​ ​gaswhlasszone.​ ​gascldiv class -/="active"> - Timezone-offlas-gass="clr class=lasszone.​offlas.​gaswhlasszone.​offlas.​gascldiv class -/="active"> - Timezone-opens="clr class=lasszone.​openwhlasszone.​opencldiv class -/="active"> - Timezone-trang an es-gass="clr class=lasszone.​trang an es.​gaswhlasszone.​trang an es.​gascldiv class -/="active"> - Timezone-vnnecd=-gass="clr class=lasszone.​vnnecd=.​gaswhlasszone.​vnnecd=.​gascldiv class -/="active"> - - becauses"st >Date/ if]- - <="aonstruct()mys="cl>Myre y="henual <="aonstruct()ime ac2 Rel"tCoe ac2nual <="aonstruct()g ae Omat we y="he g ae nual <="aonstruct()mi/dd> Mi/dd> g ae nual <="aonstruct()privacys="cl>Privacy policynualphp?id=11128 you off gfooterac - s"st ExternPlom108thirdnspaty librarif=' if]-