Showing posts with label PHP Date and Time. Show all posts
Showing posts with label PHP Date and Time. Show all posts

18 November, 2010

Subtract hours from date in php

One of my friend asked me how to subtract hours from date in php. The subtraction
could be hours or days. So here is what i did for him, to subtract hours from date
in php.
Lets say we need to subtract two hours from current date, then

//get today's date
$today = date("Y-m-d H:i:s", time());
$hours = 2;
//first convert today's date to timestamp, as
$strTodayDate = strtotime($today);
//now subtract hours from date
$subtractedTimeStr = strtotime("- $hours hours", $strTodayDate);
//now get the resulting date, i.e. date prior to 2 hours from today
$finalDate = date("Y-m-d H:i:s", $subtractedTimeStr);
echo $finalDate;

And thats it. To subtract days from date use days in place of hours in above code as:

$days = 2;
$subtractedTimeStr = strtotime("- $days days", $strTodayDate);

Similarly, to add days or hours to date we can do as:

$subtractedTimeStr = strtotime("+ $hours hours", $strTodayDate);
$subtractedTimeStr = strtotime("+ $days days", $strTodayDate);

Hope this helps someone.

31 October, 2010

Get all month names between two dates in php

Yesterday i faced a problem regarding date manipulation in php. I had to get all month names between two dates, after some work around, i managed to make it work. I created a function "get_months" and passed two dates i.e. start date and end date to find list of months between the two dates.
Here's the function:

function get_months($date1, $date2) {
//convert dates to UNIX timestamp
$time1 = strtotime($date1);
$time2 = strtotime($date2);
$tmp = date('mY', $time2);

$months[] = array("month" => date('F', $time1), "year" => date('Y', $time1));

while($time1 < $time2) {
$time1 = strtotime(date('Y-m-d', $time1).' +1 month');
if(date('mY', $time1) != $tmp && ($time1 < $time2)) {
$months[] = array("month" => date('F', $time1), "year" => date('Y', $time1));
}
}
$months[] = array("month" => date('F', $time2), "year" => date('Y', $time2));
return $months; //returns array of month names with year
}

Thats it. I hope this helps someone like me.

19 September, 2010

Get age from birthdate using php the easiest way

Once i had a situation to get age of a user from his/her birthdate. After some testing, i managed to work it out. Following
code is used to get age from date. I hope this might help someone like me.

//date in mm/dd/yyyy format; or it can be in other formats as well
$birthDate = "08/14/1972";
//explode the date to get month, day and year
$birthDate = explode("/", $birthDate);
//get age from date or birthdate
$age = (date("md", date("U", mktime(0, 0, 0, $birthDate[1], $birthDate[0], $birthDate[2]))) > date("md") ? ((date("Y")-$birthDate[2])):(date("Y")-$birthDate[2] - 1));
echo "Age is:".$age;
?>

23 May, 2010

Find number of days remain using php

Sometimes it is required to get number of days remaining from some date. Following code is used to calculate
how many days remain from some date using php.

//get number of days remaining from some date
$inpDate = "2010-06-06";
list($inpYear, $inpMonth, $inpDay) = split("[./-]", $inpDate);
$numOfDaysRemaining = ceil((mktime(0,0,0,$inpMonth, $inpDay, $inpYear) - time())/86400);
echo "Num of days remaining:".$numOfDaysRemaining;
//Similarly how many days remain from current date can also be found by setting current date to $inpDate variable
?>

Calculate age in year, month and days from birthdate using php

Following code can be used to get the age using php. Simple php date functions is be used to calculate age
from birth date using php

//find the age using php
//get the birth date
$inpDate = "1983-12-30";
//seperate the birth date day, month and year
list($inpYear, $inpMonth, $inpDay) = explode("-", $inpDate);
//get number of days in a month using following php function
$numOfDays = cal_days_in_month(CAL_GREGORIAN, $inpMonth, $inpYear);
//check if the day inputted is greater then number of days in month and set appropriately
if($inpDay > $numOfDays) {
$inpDay = $numOfDays;
}
//set month to 12 if greater then 12
if($inpMonth > 12) {
$inpMonth = 12;
}
//get the difference in year
$diffYear = date("Y") - $inpYear;
//get the difference in month
$diffMonth = date("m") - $inpMonth;
//get the day difference
$diffDay = date("d") - $inpDay;
//check if month is less than 0
if($diffMonth < 0) {
$diffYear -= 1;
$diffMonth += 12;
}
//check if the day is less than 0
if($diffDay < 0) {
$diffMonth -= 1;
$diffDay += $numOfDays;
}
echo "
";
echo $diffYear. "years";
echo "
";
echo $diffMonth." months";
echo "
";
echo $diffDay." days";
?>