The blog

PHP Basic knowledge : Playing with the date() and strtotime()

Hello all reader..,

Today I would like to suggest you about the date(); function on PHP code.
I knew many people forgotten something simple like this, included me ^__^.
So I just put some example how to playing with the date and time;

On this article you have to know 2 functions following;

date — Format a local time/date
string date ( string $format [, int $timestamp = time() ] )

Example;

<?php echo date(“l”); ?>
// Prints something like: Monday | Tuesday | Wednesday … Sunday : is depend on What’s date you try it 🙂

<?php echo date(‘l jS \of F Y h:i:s A’); ?>
// Prints something like: Saturday 29th of April 2014 11:28:00 AM

PS: you can see the meaning of date and time format ( l jS \of F Y h:i:s A ) here:
http://php.net/manual/en/function.date.php

Second..,

strtotime — Parse about any English textual datetime description into a Unix timestamp (Number that difficult to understand)
int strtotime ( string $time [, int $now ] )

Example;

<?php
echo strtotime(“now”);
echo strtotime(“today”);
echo strtotime(“tomorrow”);
echo strtotime(“today + 1 years”);
echo strtotime(“+5 hours”);
echo strtotime(“+2 weeks”);
echo strtotime(“+1 week 2 days 6 hours 30 seconds”);
echo strtotime(“next Wednesday”);
echo strtotime(“last Saturday”);
echo strtotime(“29 April 2014”);
?>

OK Then.. time to playing with two functions

It’s quite easy to use strtotime() to transforming the wording to be date variable.., But like I said before. The variable of strtotime function will be Unix timestamp (unreadable for human. -*- ).
So we have to use date() function to transform it. You can see some example below.

Example;
echo date(“jS F, Y”, strtotime(“+1 week 2 days 6 hours 30 seconds”));
// outputs 8th May, 2014

echo date(“jS F, Y”, strtotime(“next Wednesday”));
// outputs 30th April, 2014

echo date(“jS F, Y”, strtotime(“today + 1 years”));
// outputs 29th April, 2015

 

hope you will be fun with coding.
J.

Leave a comment?

You must be logged in to post a comment.

%d bloggers like this: