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 tech industry for major developments, new product launches, AI breakthroughs, video game releases and other newsworthy events. Editors assign relevant stories to staff writers or freelance contributors 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 tech headlines of the day delivered to your inbox

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

    Tech News

    Explore the latest in tech with our Tech News. We cut through the noise for concise, relevant updates, keeping you informed about the rapidly evolving tech landscape with curated content that separates signal from noise.

    In-Depth Tech Stories

    Explore tech impact in In-Depth Stories. Narrative data journalism offers comprehensive analyses, revealing stories behind data. Understand industry trends for a deeper perspective on tech's intricate relationships with society.

    Expert Reviews

    Empower decisions with Expert Reviews, merging industry expertise and insightful analysis. Delve into tech intricacies, get the best deals, and stay ahead with our trustworthy guide to navigating the ever-changing tech market.