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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

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

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

 

Would work extremely well assuming the dates entered were after 1969.

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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  "

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.