Tag "DateTime" (15)
          
          
            
            Gets the first day of the month from the given date.
            Gets the last day of month from the given date. ... Reveal Code
            
              public static class MonthExtensions
{
    /// 
    /// Gets the first day of the month.
    /// 
    /// The given date.
    /// the first day of the month
    public static DateTime GetFirstDayOfMonth(DateTime givenDate)
    {
        return new DateTime(givenDate.Year, givenDate.Month, 1);
    }
    /// 
    /// Gets the last day of month.
    /// 
    /// The given date.
    /// the last day of the month
    public static DateTime GetTheLastDayOfMonth(DateTime givenDate)
    {
        return GetFirstDayOfMonth(givenDate).AddMonths(1).Subtract(new TimeSpan(1, 0, 0, 0, 0));
    }
}
             
            
            
          
          
          
            
              C#: Calculate Age    
 
              
            
            Calculate Age ... Reveal Code
            
                  public static int GetAge(DateTime birthday)
    {
        DateTime now = DateTime.Today;
        int age = now.Year - birthday.Year;
        if (now < birthday.AddYears(age)) age--;
        return age;
    }
             
            
            
          
          
          
            
              JavaScript: Get UNIX timestamp    
 
              
            
            Get UNIX timestamp via JavaScript. Analogue for PHP's time() function: http://php.net/manual/en/function.time.php ... Reveal Code
            
              var unix_timestamp = Math.round((new Date()).getTime() / 1000);
             
            
            
          
          
          
            
              PHP: Get the user's age    
 
              
            
            Get the user's age by date of birth ... Reveal Code
            
              function getAge($birth_date) {
    return floor((time() - strtotime($birth_date)) / 31556926);
}
             
            
            
          
          
          
            
            Get zodiac sign by birth date ... Reveal Code
            
              function getZodiacSign($birth_date) {
    $bd = explode("-", $birth_date);
    $daymonth = $bd[1] . "-" . $bd[2];
    if ($daymonth >= '01-21' and $daymonth <= '02-20') {
        $zodiac = 'Aquarius';
    } elseif ($daymonth >= '02-21' and $daymonth <= '03-20') {
        $zodiac = 'Pisces';
    } elseif ($daymonth >= '03-21' and $daymonth <= '04-20') {
        $zodiac = 'Aries';
    } elseif ($daymonth >= '04-21' and $daymonth <= '05-20') {
        $zodiac = 'Taurus';
    } elseif ($daymonth >= '05-21' and $daymonth <= '06-21') {
        $zodiac = 'Gemini';
    } elseif ($daymonth >= '06-22' and $daymonth <= '07-22') {
        $zodiac = 'Cancer';
    } elseif ($daymonth >= '07-23' and $daymonth <= '08-23') {
        $zodiac = 'Leo';
    } elseif ($daymonth >= '08-24' and $daymonth <= '09-23') {
        $zodiac = 'Virgo';
    } elseif ($daymonth >= '09-24' and $daymonth <= '10-23') {
        $zodiac = 'Libra';
    } elseif ($daymonth >= '10-24' and $daymonth <= '11-22') {
        $zodiac = 'Scorpio';
    } elseif ($daymonth >= '11-23' and $daymonth <= '12-21') {
        $zodiac = 'Sagittarius';
    } else {
        $zodiac = 'Capricorn';
    }
    return $zodiac;
}
//Will return "Aquarius"
echo getZodiacSign('1982-01-27');
             
            
            
          
          
          
            
            Convert the timestamp to human-readable format like XX minutes ago or XX days ago. Was found here: http://stackoverflow.com/questions/2915864/php-how-to-find-the-time-elapsed-since-a-date-time ... Reveal Code
            
              function humanTiming ($time)
{
        $time = strtotime($time);
    $time = time() - $time; // to get the time since that moment
    $tokens = array (
        31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
    );
    foreach ($tokens as $unit => $text) {
        if ($time < $unit) continue;
        $numberOfUnits = floor($time / $unit);
        return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'').' ago';
    }
}
$time = '2013-05-25 15:45:22';
echo 'Event happened '.humanTiming($time);
             
            
            
          
          
          
            
            Session lifetime. An important point: set a directory to store sessions. ... Reveal Code
            
              ini_set('session.save_path', $_SERVER['DOCUMENT_ROOT'] . '/sessions/'); //Important!
ini_set('session.gc_maxlifetime', 86400);
ini_set('session.cookie_lifetime', 86400);
             
            
            
          
          
          
            
              Ruby: Hints for work with time in rails    
 
              
                30.05.2013 03:28 drakmail Ruby 
               
            
            Hints for work with time in rails ... Reveal Code
            
              # DO:
2.hours.ago # => Fri, 02 Mar 2012 20:04:47 JST +09:00
1.day.from_now # => Fri, 03 Mar 2012 22:04:47 JST +09:00
Date.today.to_time_in_current_zone # => Fri, 02 Mar 2012 22:04:47 JST +09:00
Date.current # => Fri, 02 Mar
Time.zone.parse("2012-03-02 16:05:37") # => Fri, 02 Mar 2012 16:05:37 JST +09:00
Time.zone.now # => Fri, 02 Mar 2012 22:04:47 JST +09:00
Time.current # Same thing but shorter. (Thank you Lukas Sarnacki pointing this out.)
Time.zone.today # If you really can't have a Time or DateTime for some reason
Time.zone.now.utc.iso8601 # When supliyng an API (you can actually skip .zone here, but I find it better to always use it, than miss it when it's needed)
Time.strptime(time_string, '%Y-%m-%dT%H:%M:%S%z').in_time_zone(Time.zone) # If you can't use Time#parse
14:27:45
# DON’Ts
Time.now # => Returns system time and ignores your configured time zone.
Time.parse("2012-03-02 16:05:37") # => Will assume time string given is in the system's time zone.
Time.strptime(time_string, '%Y-%m-%dT%H:%M:%S%z') # Same problem as with Time#parse.
Date.today # This could be yesterday or tomorrow depending on the machine's time zone.
Date.today.to_time # => # Still not the configured time zone.
             
            
            
          
          
          
            
            Convert the date/time interval in human-readable format ... Reveal Code
            
              $startDate = 'now';
$endDate = '2013-08-12 14:30:00';
function date_diff($date1, $date2) {
    $diff = strtotime($date2) - strtotime($date1);
    return abs($diff);
}
function seconds2human($ss) {
    $s = $ss % 60;
    $m = floor(($ss % 3600) / 60);
    $h = floor(($ss) / 3600);
    return "$h hours, $m minutes, $s seconds";
}
echo "This file will expire in  ". seconds2human(date_diff($startDate,$endDate));
             
            
            
          
          
          
            
            Execution time of the script. Shows the text like this: "The script has been executed in 0.0090 sec." ... Reveal Code
            
              $time_start = microtime(true);
// your PHP code here
$time_end = microtime(true);
$time = $time_end - $time_start;
echo 'This script has been executed in ' . $time . ' sec.'; 
             
            
            
          
          
          
            
              PHP: Format a date received from MySQL    
 
              
            
            Format a date/time received from MySQL in a more readable (user-friendly) form. ... Reveal Code
            
              //$row[create_datetime] == '2013-05-06 03:14:43';
//$formatted_datetime == '06.05.2013 03:14';
define('SHORTDATETIME', 'd.m.Y H:i');
$formatted_datetime = date_format(date_create($row[create_datetime]), SHORTDATETIME);
             
            
            
          
          
          
            
              MySQL: The number of records for yesterday    
 
              
            
            The number of records in the table for yesterday ... Reveal Code
            
              SELECT COUNT(id) FROM my_table WHERE created_datetime >= (DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY), '%Y-%m-%d')) AND created_datetime < (DATE_FORMAT(NOW(), '%Y-%m-%d'))
             
            
            
          
          
          
            
              MySQL: The number of records for the current month    
 
              
            
            The number of records in a table for the current month (from 1st to current day) ... Reveal Code
            
              SELECT COUNT(id) FROM my_table WHERE date_format(created_datetime, '%Y%m') = date_format(now(), '%Y%m') 
             
            
            
          
          
          
            
              PHP: Convert a date from SQL to RFC822 format    
 
              
            
            Convert a date from SQL to RFC822 format. It may be useful for creating RSS-feed. ... Reveal Code
            
              function sql_to_rfc822($sql_date_str) {
    $d1 = explode(" ", $sql_date_str);
    $d2 = explode("-", $d1[0]);
    $d3 = explode(":", $d1[1]);
    $rfc822_date_str = date("D, d M Y H:i:s", mktime($d3[0], $d3[1], $d3[2], $d2[1], $d2[2], $d2[0])) . "  GMT";
    return $rfc822_date_str;
}