php timestamp function returns a wrong value

Posted on 2009-04-29
Last Modified: 2013-12-12
I used php timestamp function. its returning values for dates and to calculate number of days between two dates.

but I observed from 8th March its not giving correct answer.

When I run this for each date and see I saw that difference of timestamps for 7th March & 8th march is not 86400. its 82800. Why is that.?

But thereafter its again give correct difference between dates. but its cause for following function to return wrong answer.

Function is to see the Week Number of any given date. Based on a given date weeks are switching every Tuesday.
function get_week_n($input_date){

	$base_date = strtotime("2009-02-24"); //which is starting the week# 01


	$cal_date = strtotime($input_date);

	$diff = $cal_date - $base_date;

	$totaldays = (int)floor($diff / (60*60*24));


	$w_l1 = (int)floor($totaldays/7);

	$w_l2 = $w_l1 % 4;

	$week_n = $w_l2+1;

	return $week_n;


Open in new window

Question by:businessesatoz
    LVL 14

    Expert Comment


    Its working correct

    Check that the following:
            $base_date = strtotime("2009-04-7"); //which is starting the week# 01
            $input_date = '2009-04-8';
            $cal_date = strtotime($input_date);
            $diff = $cal_date - $base_date;
            $totaldays = (int)floor($diff / (60*60*24));
            $w_l1 = (int)floor($totaldays/7);
            $w_l2 = $w_l1 % 4;
            $week_n = $w_l2+1;
    	echo "<br>",$base_date;
    	echo "<br>",$cal_date;
    	echo "<br>:",$diff;
    	echo "<br>Days:", $totaldays;
            echo "<br>No:", $w_l2;
            return $week_n;

    Open in new window

    LVL 50

    Accepted Solution

    >>>  I saw that difference of timestamps for 7th March & 8th march is not 86400. its 82800. Why is that.?

    86400-82800=3600, which happens to be the same number of seconds in an hour.  Spring forward, fall back...daylight savings time made you 'lose' an hour.

    Author Comment


    base date should start from 2009-02-24.

    if you put $input_date='2009-04-28' it should be 63 days. but this returns 62 days and calculate. thats wrong.

    but if you try it in localhost its return results with 63. this is happening when its uploaded to server.

    I need it to calculate it with 63 days.
    LVL 19

    Expert Comment

    Don't rely on seconds use days instead
    $tomorrow  = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
    $lastmonth = mktime(0, 0, 0, date("m")-1, date("d"),   date("Y"));
    $nextyear  = mktime(0, 0, 0, date("m"),   date("d"),   date("Y")+1);

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now