New Website

I've made a new website, as lynx.io is dead. You can find it here: http://macr.ae/—it's similar in format to lynx.io, but has better articles, and they're all written by me.

JavaScript disabled

While it will still mostly work, a lot of this site's functionality relies on JavaScript - please enable it for the best experience.

Working with date and time

There are many function in PHP for working with date and time. In this article, I will be explaining the most commonly used ones. Please note that the dates are in DD/MM/YYYY format, eg 31/12/2012 (I'm British).

time()

The time function is the date and time related function that I use most frequently, and is probably up there among my most commonly used PHP functions. It takes no parameters, it simply returns the current unix timestamp - the amount of seconds since the Unix Epoch (1st of January 1970, 00:00:00 GMT). For example, this site opened at 1293843661 - 01/01/11 01:01:01.

<?php echo time(); ?>

microtime()

microtime returns the unix time with microseconds as a string:

<?php echo microtime(); ?>

At the time of writing, that returned 0.59021500 1293714593 - 1293714593 is the timestamp, and 0.59021500 is the miliseconds (units: seconds). You can also use microtime(true), which would return 1293714593.590 as a float. microtime is good for timing how long some code takes to execute:

<?php

$starttime = microtime(true);

// some code here

echo 'Time taken: ' . (microtime(true) - $starttime);

That exact code returns 0.00022006034851074 for me, but obviously it depends what code you put between the two microtime calls, and the hardware that you're running the code from.

strtotime()

strtotime takes a string and converts it to a timestamp. It's pretty simple:

<?php

echo strtotime('now') . PHP_EOL;
echo strtotime('25 December 2010') . PHP_EOL;
echo strtotime('+1 day') . PHP_EOL;
echo strtotime('+2 weeks 2 days 2 hours 2 seconds') . PHP_EOL;
echo strtotime('next Thursday') . PHP_EOL;
echo strtotime('last Thursday');

This would return something like this:

1324508816
1293235200
1324595216
1325898418
1324512000
1323907200

You can check to see whether it has worked using the following code:

<?php

$str = 'This string clearly isn\'t a date';

if (!($timestamp = strtotime($str))) {
    echo '$str isn\'t a timestamp';
} else {
    echo '$str is a timestamp - ' . date('l dS \o\f F Y h:i:s A', $timestamp);
}

date()

The date function formats a unix timestamp to a human readable date, like 31/12/2010 or 31 December 2010. It takes two arguments, syntax and timestamp, where timestamp is optional. If timestamp isn't specified, it uses the current timestamp.

Example:

<?php echo date('d/m/Y'); ?>

That will echo 30/12/2010. The weird string, d/m/Y, is the format of date you want returning. The most commonly used are as follows:

You can find the full list of the letters used for various outputs here.

checkdate()

checkdate simply validates a Gregorian date. It returns a boolean value. It works as follows:

<?php

var_dump(checkdate(12, 31, 2010));  // true
var_dump(checkdate(31, 13, 2010));  // false

mktime()

mktime turns a date into a unix timestamp. It is similar to strtotime but works differently.

<?php

$hour = 12;
$minute = 23;
$second = 0;
$month = 9;
$day = 28;
$year = 2010;

echo mktime($hour, $minute, $second, $month, $day, $year);

That code returns 1285676580. If you miss out arguments, it defaults to the current time / date. Some websites still tell you about $is_dst as the last argument, but that was deprecated a few years back.

About Callum Macrae:

Callum Macrae is the founder of lynx.io and a JavaScript developer from the United Kingdom. He is currently writing his first book, to be published by O'Reilly Media.

You can view more articles by this author here.

Tags: functions php date time

Comments

No comments; leave one below.

says:

Add comment

 

You can use markdown in comments (press "m" for a cheatsheet).

Enable JavaScript to post a comment

Markdown Cheat Sheet

# This is an <h1> tag
## This an <h2> tag
###### This is an <h6> tag

Inline markup: _this text is italic_, **this is bold**, and `code()`.

[Link text](link URL "Optional title")
[Google](http://google.com/ "Google!")

![Alt text](image URL)

![This is a fish](images/fish.jpg)

1. Ordered list item 1
2. Ordered list item 2

* Unordered list item 1
* Unordered list item 2
* Item 2a
* Item 2b

And some code:

// Code is indented by one tab
echo 'Hello world!';

Horizontal rules are done using four or more hyphens:

----

> This is a blockquote

This is an <h1> tag

This an <h2> tag

This is an <h6> tag

Inline markup: this text is italic, this is bold, and code().

Link text Google

This is a fish

  1. Ordered list item 1
  2. Ordered list item 2
  • Unordered list item 1
  • Unordered list item 2
    • Item 2a
    • Item 2b

And some code:

// Code is indented by one tab
echo 'Hello world!';

Horizontal rules are done using four or more hyphens:


This is a blockquote

Toggle MarkDown / HTML (t), full reference or close this