Home Tips for Working With Dates in PHP

Tips for Working With Dates in PHP

I’ve often found dates in PHP to be a bit of a pain to manage. A variety of similar functions in the language make it unnecessarily complicated to do what you need to do.

Getting the current date is one thing, but how about adding a set number of days to a date in the future? It’s possible, but not particularly intuitive. Here are a few tips to make life easier when dealing with dates in PHP.

Look At What You Want To Do

Breaking down the task at hand is always a good way to approach a problem. Do you need to get the current date? Are you doing any calculations or are you reformatting the date? It’s usually easier to do all the calculations first and worry about how the date is displayed last.

Write Unit Tests

Without unit tests, you’ll be copying around code that serves a specific purpose instead of writing small, testable methods that you can easily reuse. Unit testing is one way to make dates a lot easier to manage.

Allow A Date Override For Your Date Handling Methods

If you’re writing any code that handles dates, ensure you can pass in a specific date instead of relying on the current date.

For instance, I have a method that calculates the turnaround time for a print order, and it usually takes the current date and adds several days to it. (It has to skip weekends, which is why it’s not as simple as adding a set number of days.) This method is very easy to test because you can pass in a specific date with the number of turnaround days and verify the date that is returned.

Use Zend_Date

Zend_Date is the Zend Framework approach. This makes light work of handling dates in PHP.

Use a Date Picker

There’s a nice date picker in jQuery. This is more beneficial to help users when inputting dates, but it should reduce your workload too.

Photo by kevindooley

About ReadWrite’s Editorial Process

The ReadWrite Editorial policy involves closely monitoring the gambling and blockchain industries for major developments, new product and brand launches, game releases and other newsworthy events. Editors assign relevant stories to in-house staff writers with expertise in each particular topic area. Before publication, articles go through a rigorous round of editing for accuracy, clarity, and to ensure adherence to ReadWrite's style guidelines.

Get the biggest iGaming headlines of the day delivered to your inbox

    By signing up, you agree to our Terms and Privacy Policy. Unsubscribe anytime.

    Gambling News

    Explore the latest in online gambling with our curated updates. We cut through the noise to deliver concise, relevant insights, keeping you informed about the ever-changing world of iGaming and its most important trends.

    In-Depth Strategy Guides

    Elevate your game with tailored strategies for sports betting, table games, slots, and poker. Learn how to maximize bonuses, refine your tactics, and boost your chances to beat the house.

    Unbiased Expert Reviews

    Honest and transparent reviews of sportsbooks, casinos and poker rooms crafted through industry expertise and in-depth analysis. Delve into intricacies, get the best bonus deals, and stay ahead with our trustworthy guides.