Creating Date Function OOPS way in PHP 5 - Mostlikers

09 March, 2017

Creating Date Function OOPS way in PHP 5

Today we are going to see How to create date function oops way in PHP. The Date/Time PHP extension is a set of classes it's allowed to work date and time-related tasks. based oops way we can get calculate working days between two dates, calculate age given date of birth, based on date find between two dates in years months and days.For using this function We can Easily calculate data functions.




Based on this feature we can get A timezone of the world is represented by a DateTimeZone object. The format takes default system time zone. Now let we start about oops way Date and time function.

Whenever we want to display date just we use the Date() date function. But you can't get the split of date, month, year format. For using DateTime() Function you can get the separate day,date,month,year. DateTime() one function split all the formats.

DateTime::__construct() example

<?php
    $date = new DateTime('2017-01-01');
    echo $date->format('Y-m-d');
?>


The DateTime construct default set with Now(). Just call new DateTime() it will display the current date.


Calculate age based on date of birth

For Calculate age based on a date of birth, Compare today date between user birthday date. diff() will calculate between days.
$bday = new DateTime('02.12.1992');
$today = new DateTime('00:00:00');
$diff = $today->diff($bday);
printf('%d years, %d month, %d days', $diff->y, $diff->m, $diff->d);

Finding Two Dates between day's

This scenario might be comparing two dates similar like above function. Day's will calculate between 2 dates. 
$bday = new DateTime('02.12.1992');
$today = new DateTime('00:00:00');
echo $diff = $today->diff($bday)->format("%a");

Finding the two days between time.

This generally needs for offer advertisement announcement website need this script. Once valid time offer ends it's should close the page, That scenario they will compare the time between the date. 

$bday = new DateTime($date);
$today = new DateTime('00:00:00');
$diff = $today->diff($bday);
$hours = $diff->h;
$hours = $hours + ($diff->days*24);
echo number_format($hours);

The hours calculate multiple * numbers of the day. $diff->h display the hours of the total day.

Setting Date Timezones

When you used DateTime() function it will take defult php.ini timezone. if you need set timezone use date_default_timezone_set() with your time zone location.
date_default_timezone_set('America/New_York');
new DateTime('today'); //datetime object is on New York time

For setting timezone based on your location, Just read this http://www.php.net/manual/en/timezones.php documentation.

Compare Two dates

I referred some tutorial compare 2 different birthdays code.
<?php
$karthi = new DateTime('02-12-1992');
$arun   = new DateTime('20-05-1991');
if ($karthi > $arun)
    echo 'Karthick is younger than arun';
?>
Try Yourself >>

We hope this basic tutorial helpful for you. Thanks for visiting keep in touch with us. Share your comments and feedback to the comments box.

No comments:

Post a comment