Jump to content

[SOLVED] function like to_days() in php


prince198

Recommended Posts

here it is

<?php

//takes a mysql date format yyyy-mm-dd

function to_days($date)
{
    $bits  = explode('-', $date, 2);
    $year = $bits[0];
    if(is_leap_year($year))
    {
        $bits[0] = '2000';
    }
    else{
        $bits[0] = '1999';
    }
    $date = implode('-',$bits);
    $leaps = 387;	//leap years up to 1600
    for($i = 1600; $i < $year; $i++)
    {
        if(is_leap_year($i))
        {
            ++$leaps;
        }
    }
    $days = date('z', strtotime($date));
    return $leaps + ($year * 365) + $days + 1;
}

function is_leap_year($year)
{
    if($year % 100 == 0 && $year % 400 == 0)
    {
        return true;	
    }
    if($year % 100 == 0)
    {
        return false;	
    }
    if($year % 4 == 0)
    {
        return true;	
    }
    return false;
}
?>

 

this works for dates after 1600-01-01. for earlier dates change:-

 

<?php
$leaps = 387;	//leap years up to 1600
for($i = 1600; $i < $year; $i++)
{
    if(is_leap_year($i))
    {
        ++$leaps;
    }
}
?>

 

to

 

<?php
$leaps = 0;	
for($i = 1; $i < $year; $i++)
{
    if(is_leap_year($i))
    {
        ++$leaps;
    }
}
?>

 

hope this helps

Erm, I imagine the strtotime(); function could help..

 

<?php
$date = "08/23/1990";
$days = strtotime($date) / (60 * 60 * 24);
?>

 

Or you could make a function of it if you wish

 

<?php
function to_days($date) 
{
$r = strtotime($date) / (60*60*24);
return $r;
}

 

So the first code would then be

 

<?php
$days = to_days("08/23/1990");
?>

 

I tried it with your sample data - didn't work lol, sorry lol.

Erm, I imagine the strtotime(); function could help..

 

<?php
$date = "23/08/1990";
$days = strtotime($date) / (60 * 60 * 24);
?>

 

But that's days since 1970, not exactly what the OP wants

 

 

Tbh, I foolishly never looked at what the function he wanted actually did and just took a guess lol..

Erm, I imagine the strtotime(); function could help..

 

<?php
$date = "23/08/1990";
$days = strtotime($date) / (60 * 60 * 24);
?>

 

But that's days since 1970, not exactly what the OP wants

 

 

with a slight adjustment, given that

SELECT TO_DAYS('1969-12-31')

--> 719527 

 

<?php
$days = to_days("23/08/1990");

function to_days ($date)
{
    $days = (strtotime($date) / (60 * 60 * 24));
    return 719527 + $days;
}
?>

Well, apart from the the offset being a day out (should be 719528) it works fine for my DOB

[pre]

mysql> SELECT TO_DAYS('1949-01-22');

+-----------------------+

| TO_DAYS('1949-01-22') |

+-----------------------+

|                711879 |

+-----------------------+

1 row in set (0.02 sec)

[/pre]

 

<?php
$days = to_days("01/22/1949");
echo $days;                              // --> 711879


function to_days ($date)
{
    $days = (strtotime($date) / (60 * 60 * 24));
    return 719528 + $days;
}
?>

The Unix epoch is 1970-2037, 67 years, so the lower bound will be somewhere in 1903 (1970 minus 67)

 

EDIT: I just checked the manual - the earliest date would be 13 Dec 1901

 

Note: The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.

hi everybody

i tried all function

first function that ( the shig  ) had writen work exactly

for the rest don't work and don't put the result exactly some functions turn float result

like 733405.333333

i know there is round in php but its possible turn not result like mysql

thank a lot for all your help and specially "the shig  "

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.