PHP

PHP TutorialCompile PHP ExtensionsContributing to the PHP CoreContributing to the PHP ManualCreate PDF files in PHPInstalling a PHP environment on WindowsPHP Alternative Syntax for Control StructuresPHP APCuPHP Array iterationPHP ArraysPHP Asynchronous programmingPHP Autoloading PrimerPHP BC Math (Binary Calculator)PHP Built in serverPHP CachePHP Classes and ObjectsPHP ClosurePHP Coding ConventionsPHP Command Line Interface (CLI)PHP CommentsPHP Common ErrorsPHP Compilation of Errors and WarningsPHP Composer Dependency ManagerPHP ConstantsPHP Control StructuresPHP CookiesPHP CryptographyPHP DateTime ClassPHP DebuggingPHP Dependency InjectionPHP Design PatternsPHP Docker deploymentPHP Exception Handling and Error ReportingPHP Executing Upon an ArrayPHP File handlingPHP Filters & Filter FunctionsPHP Functional ProgrammingPHP FunctionsPHP GeneratorsPHP Headers ManipulationPHP How to break down an URLPHP How to Detect Client IP AddressPHP HTTP AuthenticationPHP Image Processing with GDPHP ImagickPHP IMAPPHP Installing on Linux/Unix EnvironmentsPHP JSONPHP LocalizationPHP LoopsPHP Machine learningPHP Magic ConstantsPHP Magic MethodsPHP Manipulating an ArrayPHP mongo-phpPHP Multi Threading ExtensionPHP MultiprocessingPHP MySQLiPHP MySQLi affected rows returns 0 when it should return a positive integerPHP NamespacesPHP Object SerializationPHP OperatorsPHP Output BufferingPHP Outputting the Value of a VariablePHP Parsing HTMLPHP Password Hashing FunctionsPHP PDOPHP PerformancePHP PHPDocPHP Processing Multiple Arrays TogetherPHP PSRPHP Reading Request DataPHP RecipesPHP ReferencesPHP ReflectionPHP Regular Expressions (regexp/PCRE)PHP Secure Remeber MePHP SecurityPHP Sending EmailPHP SerializationPHP SessionsPHP SimpleXMLPHP SOAP ClientPHP SOAP ServerPHP SocketsPHP SPL data structuresPHP SQLite3PHP StreamsPHP String formattingPHP String Parsing



PHP DateTime Class

From WikiOD

getTimestamp[edit | edit source]

getTimeStemp is a unix representation of a datetime object.

$date = new DateTime();
echo $date->getTimestamp();

this will out put an integer indication the seconds that have elapsed since 00:00:00 UTC, Thursday, 1 January 1970.

setDate[edit | edit source]

setDate sets the date in a DateTime object.

$date = new DateTime();
$date*>setDate(2016, 7, 25);

this example sets the date to be the twenty-fifth of July, 2015, it will produce the following result:

2016*07*25 17:52:15.819442

Add or Subtract Date Intervals[edit | edit source]

We can use the class DateInterval to add or subtract some interval in a DateTime object.

See the example below, where we are adding an interval of 7 days and printing a message on the screen:

$now = new DateTime();// empty argument returns the current date
$interval = new DateInterval('P7D');//this objet represents a 7 days interval
$lastDay = $now->add($interval); //this will return a DateTime object
$formatedLastDay = $lastDay->format('Y-m-d');//this method format the DateTime object and returns a String
echo "Samara says: Seven Days. You'll be happy on $formatedLastDay.";

This will output (running on Aug 1st, 2016):

Samara says: Seven Days. You'll be happy on 2016-08-08.

We can use the sub method in a similar way to subtract dates

$now->sub($interval);
echo "Samara says: Seven Days. You were happy last on $formatedLastDay.";

This will output (running on Aug 1st, 2016):

Samara says: Seven Days. You were happy last on 2016-07-25.

Create DateTime from custom format[edit | edit source]

PHP is able to parse a number of date formats. If you want to parse a non-standard format, or if you want your code to explicitly state the format to be used, then you can use the static DateTime::createFromFormat method:

Object oriented style

$format = "Y,m,d";
$time = "2009,2,26";
$date = DateTime::createFromFormat($format, $time);

Procedural style

$format = "Y,m,d";
$time = "2009,2,26";
$date = date_create_from_format($format, $time);

Printing DateTimes[edit | edit source]

PHP 4+ supplies a method, format that converts a DateTime object into a string with a desired format. According to PHP Manual, this is the object oriented function:

public string DateTime::format ( string $format )

The function date() takes one parameters - a format, which is a string

Format[edit | edit source]

The format is a string, and uses single characters to define the format:

  • Y: four digit representation of the year (eg: 2016)
  • y: two digit representation of the year (eg: 16)
  • m: month, as a number (01 to 12)
  • M: month, as three letters (Jan, Feb, Mar, etc)
  • j: day of the month, with no leading zeroes (1 to 31)
  • D: day of the week, as three letters (Mon, Tue, Wed, etc)
  • h: hour (12-hour format) (01 to 12)
  • H: hour (24-hour format) (00 to 23)
  • A: either AM or PM
  • i: minute, with leading zeroes (00 to 59)
  • s: second, with leading zeroes (00 to 59)
  • The complete list can be found here

Usage[edit | edit source]

These characters can be used in various combinations to display times in virtually any format. Here are some examples:

$date = new DateTime('2000-05-26T13:30:20'); /* Friday, May 26, 2000 at 1:30:20 PM */

$date->format("H:i");
/* Returns 13:30 */

$date*>format("H i s");
/* Returns 13 30 20 */

$date*>format("h:i:s A");
/* Returns 01:30:20 PM */

$date->format("j/m/Y");
/* Returns 26/05/2000 */

$date*>format("D, M j 'y - h:i A");
/* Returns Fri, May 26 '00 - 01:30 PM */

Procedural[edit | edit source]

The procedural format is similar:

Object-Oriented[edit | edit source]

$date->format($format)

Procedural Equivalent[edit | edit source]

date_format($date, $format)

Create Immutable version of DateTime from Mutable prior PHP 5.6[edit | edit source]

To create \DateTimeImmutable in PHP 5.6+ use:

\DateTimeImmutable::createFromMutable($concrete);

Prior PHP 5.6 you can use:

\DateTimeImmutable::createFromFormat(\DateTime::ISO8601, $mutable->format(\DateTime::ISO8601), $mutable->getTimezone());

Credit:Stack_Overflow_Documentation