Welcome Guest, Not a member yet? Register   Sign In
Just a little date extension.
#1

[eluser]Zack Kitzmiller[/eluser]
seriously, nothing fancy. just helpful. Allows for you to figure out relative dates. I use this helper all the time, so I thought you guys and gals may want to use it..

I know it could be a lot better, and check time preferences and what not, but nothing I do is that critical. Lemme know what you think.
Code:
<?php   if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//---------- PAST Dates ----------

/**
* Get "yesterday"
*
* Returns yesterday's timestamp
*
* @access    public
* @return    integer
*/    
if ( ! function_exists('yesterday'))
{
    function yesterday()
    {
    return mktime(0, 0, 0, date("m"), date("d") - 1, date("Y"));
  }
}

/**
* Get "last_week"
*
* Returns last week timestamp
*
* @access    public
* @return    integer
*/    
if ( ! function_exists('two_weeks_previous'))
{
    function last_week()
    {
    return mktime(0, 0, 0, date("m"), date("d") - 7, date("Y"));
  }
}

/**
* Get "two_weeks_previous"
*
* Returns yesterday's timestamp
*
* @access    public
* @return    integer
*/    
if ( ! function_exists('two_weeks_previous'))
{
    function two_weeks_previous()
    {
    return mktime(0, 0, 0, date("m"), date("d") - 14, date("Y"));
  }
}

/**
* Get "last_month"
*
* Returns last month's timestamp
*
* @access    public
* @return    integer
*/    
if ( ! function_exists('last_month'))
{
    function last_month()
    {
    return mktime(0, 0, 0, date("m") - 1, date("d"), date("Y"));
  }
}

/**
* Get "last_year"
*
* Returns last month's timestamp
*
* @access    public
* @return    integer
*/    
if ( ! function_exists('last_month'))
{
    function last_year()
    {
    return mktime(0, 0, 0, date("m"), date("d"), date("Y") - 1);
  }
}
//---------- FUTURE DATES --------

/**
* Get "tomorrow"
*
* Returns tomorrow's timestamp
*
* @access    public
* @return    integer
*/    
if ( ! function_exists('tomorrow'))
{
    function tomorrow()
    {
    return mktime(0, 0, 0, date("m"), date("d") + 1, date("Y"));
  }
}

/**
* Get "next_week"
*
* Returns next_week's timestamp
*
* @access    public
* @return    integer
*/    
if ( ! function_exists('next_week'))
{
    function next_week()
    {
    return mktime(0, 0, 0, date("m"), date("d") + 7, date("Y"));
  }
}

/**
* Get "next_month"
*
* Returns next_month timestamp
*
* @access    public
* @return    integer
*/    
if ( ! function_exists('next_month'))
{
    function next_month()
    {
    return mktime(0, 0, 0, date("m") + 1, date("d"), date("Y"));
  }
}

/**
* Get "next_year"
*
* Returns next_year timestamp
*
* @access    public
* @return    integer
*/    
if ( ! function_exists('next_year'))
{
    function next_year()
    {
    return mktime(0, 0, 0, date("m"), date("d"), date("Y") + 1);
  }
}
#2

[eluser]xwero[/eluser]
why not use the strtotime function then you don't need to load a helper
Code:
$yesterday = strtotime('-1 day');
$last_week = strtotime('-1 week');
$two_weeks_ago = strtotime('-2 week');
$last_month = strtotime('-1 month');
$last_year = strtotime('-1 year');
For the future dates replace - with +.

I think the function parameter is close enough to human readable text for even beginners to know what the function does.
#3

[eluser]Zack Kitzmiller[/eluser]
[quote author="xwero" date="1245724714"]why not use the strtotime function then you don't need to load a helper
Code:
$yesterday = strtotime('-1 day');
$last_week = strtotime('-1 week');
$two_weeks_ago = strtotime('-2 week');
$last_month = strtotime('-1 month');
$last_year = strtotime('-1 year');
For the future dates replace - with +.

I think the function parameter is close enough to human readable text for even beginners to know what the function does.[/quote]

Well, I guess because I didn't read the documentation on the strtotime page on php.net, I wrote that helper along time ago when I was just starting php, so never really saw a reason to look further.

Thank's for the advice Wink
#4

[eluser]xwero[/eluser]
With the wealth of functions in php you should always check the documentation to see if you are not re-inventing the wheel. For quick checking you can read the function overview page, for example datetime function overview page
#5

[eluser]Iverson[/eluser]
Thanks. Should be helpful to somebody. Even the way you're doing it. One thing though, you have if function_exists('two_weeks_previous') for the last_week() function. You're also checking the last_month() function for the last_year() function. Or maybe I'm missing your logic behind it. Either way, good contribution.
#6

[eluser]sophistry[/eluser]
hi, thanks for putting your stuff out there! it's good to give back to the world. Wink even if cantankerous carpers like me tear it apart.

xwero's advice to trend toward concision is well taken. use strtotime()

FWIW, do not use this code for anything date critical; this code is riddled with problems that will arise on these dates: new year's eve, new year's day, leap years, februaries, daylight savings switchover days, etc... dealing with dates is maddening and you have to pay attention to details, get experience, and most of all, test, test, test.

also, why is every "time" parameter of mktime set to midnight? that is going to cause problems.




Theme © iAndrew 2016 - Forum software by © MyBB