Home > Project > pt_date: Work with dates in php

pt_date: Work with dates in php

One of the most awful things when I work in PHP is to manage operations with dates. With the PHP 5 release came the announce of the new Date Class with operatios to add days,months, years, etc… to get the diff between to dates, and more and more. But when you try to use this ‘wished’ class, what happens ? At least for me frustation. How can the php team believe that the next code is ‘human readable’ or usable?


$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7Y5M4DT4H3M2S'));
echo $date->format('Y-m-d H:i:s') . "\n"; -> 2007-06-05 04:03:02

Oh my god, in my opinion this is the wrong way to help developers.

How I need to operate frequently with dates and I don’t want to use the native Date class, I have created my own class with clear function names and with chaining feature implemented.

Let me show some small examples:


- Add 2 months to the current date

date = new pt_date();
date->add_months(2);

- Move to the begin of the month and add 5 hours:

date = new pt_date();
date->move_to_begin_of_month()->add_hours(5);

- Get days between 2 dates:

date1 = new pt_date("2009-10-15 00:00:00");
date2 = new pt_date("2009-10-22 00:00:00");
echo $date1->days_between($date2); --> 7

or

date1 = new pt_date("2009-10-15 00:00:00");
echo $date1->days_between("2009-10-22 00:00:00"); --> 7

I have published the code in github as a public open source project.

[ https://github.com/gramos74/pt_date ]

I hope that you find it useful.

Advertisements
Categories: Project Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: