mr897
March 19, 2015, 7:55pm
1
I am new to PHP and I am trying to develop a form to where someone can place two different dates in a form and get the total days of the two dates. When the form is submitted the form should be displayed as : May 1, 2012 - Jun 1, 2012 total 30 days. I know how to code a basic form but I am not sure how to code it with dates. I know about the date function, but I have never used date for a certain date. If someone can help me or show me an example I would appreciate it.
Thanks,
M
Hi mr897,
Welcome to SitePoint.
PHP dates are not easy but there are numerous very good examples available by searching “PHP manual date()”.
Another useful function that returns an integer of the current Unix timestamp is “Php manual strtotime()”.
$str = "+1 week 2 days 4 hours 2 seconds";
$secs = strtotime($str);
$date = date('Y - m - d H:i:s', $secs);
echo '<br /><b>$str = </b>' .$str;
echo '<br /><b>$secs = </b>' .$secs;
echo '<br /><b>$date = </b>' .$date;
added
$x = 'May 1, 2012';
$x1 = strtotime($x);
$y = 'Jun 1, 2012';
$y1 = strtotime($y);
$z = (int) $y1 - (int) $x1; // secs
echo '<br />$x1 = ' .$x1;
echo '<br />$y1 = ' .$y1;
echo '<br />difference (secs) = ' .$z;
echo '<br />difference (mins) = ' .$z / (60);
echo '<br />difference (hour) = ' .$z / (60*60);
echo '<br />difference (days) = ' .$z / (60*60*24);
Output:
$str = +1 week 2 days 4 hours 2 seconds
$secs = 1427628630
$date = 2015 - 03 - 29 18:30:30
May 1, 2012 = 1335805200
Jun 1, 2012 = 1338483600
difference (secs) = 2678400
difference (mins) = 44640
difference (hour) = 744
difference (days) = 31
Above is just one of many ways to obtain the difference between two dates.
1 Like
the other notable variant includes the DateTime
classes.
mr897
March 20, 2015, 5:25pm
4
Thanks everyone for all the good information. I will take a closer look at your suggestions.
Thanks,
mr897
system
Closed
June 20, 2015, 12:37am
5
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.