WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Comparing Dates
Categories : PHP, Date Time Click here to Update Your Picture
Justin French
Date : May 05th 2003
Grade : 5 of 5 (graded 6 times)
Viewed : 13357
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Justin French
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

PHP has got some awesome functions working with dates... it's just a little daunting to know which
ones to use at first.

IMHO, the easiest way to work with dates is if they're in Unix timestamp format (total number of
seconds since the Unix epoch (Jan 1, 1970)).

You can also get a timestamp for "now" on the server using time()

So, first thing we need to do is take your existing date, and get it into a timestamp.

You might have dates in many formats:
YYYY-MM-DD
YY-MM-DD
MM-DD-YY
MM/DD/YY
MM-DD-YYYY
MM/DD/YYYY
DD-MM-YY
DD-MM-YYYY
DD/MM/YY
DD/MM/YYYY
Or even something natural language like 'December 10, 2002'

PHP's strtotime() function takes a wide variety of natural language and date formates, converting
them to timestamps.

<?php
$stamp
= strtotime('2003-04-28');
$stamp = strtotime('last month');
?>


In my experience, you need to test what sort of results you get with strtotime, depending on your
format. strtotime() is a little bit US-centric, so it doesn't work with DD/MM/YY formats at all.
You need to switch the day and month around:

<?php
$nonUSdate
= '27/06/77';
list(
$day,$month,$year) = explode('/',$nonUSdate);
$newDate = "{$month}/{$day}/{$year}";
$stamp = strtotime($newDate);
?>


You can read more about strtotime(), with plenty of examples at
http://www.weberdev.com/strtotime

Now that you can convert your dates to timestamps, it's really easy to compare them:
<?php
$date1
= strtotime('06/27/1977');
$date2 = strtotime('12/18/2003');

if(
$date1 > $date2) { ... }
if(
$date1 < time()) { ... }
if(
$date2 >= $date1) { ... }
?>


It's also really easy to add, subtract or divide by days/hours/minutes

There are 60 seconds in a minute, 60 minutes in an hour, and 24 hours in a day = 86400seconds

<?php
// add an hour to a stamp
$date1 = $date1 + 3600;

// subtract a week
$date1 = $date1 - 2073600;
?>



Best of all, PHP's date() function accepts unix timestamps as it's second argument, so you easily
make the timestamps human-readable again:

<?php
echo date('Y-m-d',$mystamp);
?>


Check out these pages for more info, and do some experimenting:

http://www.weberdev.com/date
http://www.weberdev.com/strtotime
http://www.weberdev.com/time
http://www.weberdev.com/explode













A Timing Class
Categories : PHP, PHP Classes, Date Time
Finding the day of the week for a specific date.
Categories : PHP, Databases, MySQL, Date Time
A wrapper function to format dates coming from a databases with the same syntax as PHP's date() function.
Categories : Date Time, Databases, PHP
pcCalendar class - Allows for the creation of calendars in HTML pages. All output functions can be easily overridden, refer to article 1471 for an example.
Categories : PHP, Date Time, Calendar, PHP Classes
A PHP Calendar function with CSS : add a cool calendar to any php page by just adding a calendar class based function.
Categories : PHP, PHP Classes, Calendar, Date Time
PHP Round Clock - Must have Gif support to use this.
Categories : PHP, Date Time, Graphics
A simple and fast calendar combining PHP and tables. Use this as a base for applications in which a calendar is needed.
Categories : Date Time, PHP, Complete Programs, Calendar
Open and Close your website in fixed times .
Categories : PHP, PHP Classes, Cron, Date Time
mysql date/time converters
Categories : PHP, MySQL, Databases, Date Time
Creates three SELECT form fields: Month, Day, and Year. You give it a string which will be used to make the name for the three fields, and a number of seconds to use as the default date. If you give it blank for this value, the current date is used.
Categories : HTML and PHP, PHP, Date Time
If you want to create select buttons featuring current date this example will show you how...
Categories : Date Time, HTML and PHP, PHP
Example of using the pcCalendar class, article 1468 on weberdev.com. Calendar example.
Categories : PHP, Date Time, PHP Classes, Calendar
calculus of the eastersunday
Categories : BC math, PHP, Date Time, Databases
Functions used to define a schedule of holidays. Can define non-fixed holidays (eg. 3rd sunday of June).
Categories : Calendar, Date Time, PHP
Monthly and Daily Upcoming Events calendar.
Categories : Date Time, PostgreSQL, PHP, Calendar, Databases
 José Santos wrote : 966
It`s very useful in PHP, and it`s used in somme mutch Websites and Intranet`s !

I have question: how do you calculate 2073600 ?
I say that a minute have 60 seconds, a hour 60 minutes and, a day 24 hours.
But I dont get this result !!
 
 mick 28 wrote :967
60 seconds = 1 minute
3600 seconds = 1 hour (60 minutes)
86400 seconds = 1 day (24 hours)
604800 seconds = 1 week (7 days)
2419200 seconds = 1 Lunar Month (28 days)

Guess it`s been a long week for Justin :-)