Tag "Date Routines" (8)
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);
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));
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;
}