Today I thought I can share you a few of my functions. Today’s article is an easy one, but I use the functions in almost all my websites, so I thought they can be useful to you.
So here are a few functions that get the year,month,day,hour,minute,second from a time stamp, similar to the MYSQL functions. I like this approach a lot, that’s why I wrote these functions.
function day($ts)
{
return date("d", $ts);
}
function month($ts)
{
return date("m", $ts);
}
function year($ts)
{
return date("Y", $ts);
}
function hour($ts)
{
return date("H", $ts);
}
function minute($ts)
{
return date("i", $ts);
}
function second($ts)
{
return date("s", $ts);
}
function this_year()
{
return date("Y");
}
function this_month()
{
return date("m");
}
function this_day()
{
return date("d");
}
function this_hour()
{
return date("H");
}
function this_minute()
{
return date("i");
}
function this_second()
{
return date("s");
}
And now, for the OOP gurus here is a class with static members, that can be used in your applications.
class MyDate
{
public static function day($ts)
{
return date("d", $ts);
}
public static function month($ts)
{
return date("m", $ts);
}
public static function year($ts)
{
return date("Y", $ts);
}
public static function hour($ts)
{
return date("H", $ts);
}
public static function minute($ts)
{
return date("i", $ts);
}
public static function second($ts)
{
return date("s", $ts);
}
function this_year()
{
return date("Y");
}
function this_month()
{
return date("m");
}
function this_day()
{
return date("d");
}
function this_hour()
{
return date("H");
}
function this_minute()
{
return date("i");
}
function this_second()
{
return date("s");
}
function addZero($ent)
{
if((int)trim($ent)<10)
return "0".(int)trim($ent);
else return trim($ent);
}
}
The addZero function adds a ‘0′ in front of number smaller than 10. It is useful when you get the month, or the day and it has no ‘0′. It is used mostly for displaying date.
Usage:
$time = time();
$day = day($time);
//for the OOP fans
$day = MyDate::day($time);