Some PHP Date and time classic code applications
PHP Date and some classic code applications
First day of week
<?php
$first_day_of_week = mktime(0, 0, 0, date('m'), date('d')-date('N')+1, date('Y'));
echo date('Y-m-d H:i:s', $first_day_of_week);
//Depending on server settings and location date('N') can be changed to date('w') depending on Monday or Sunday is considered to be the first day of week.
?>First timestamp of today
<?php
$first_timestamp_of_today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
echo date('Y-m-d H:i:s', $first_timestamp_of_today); //2010-12-15 00:00:00
?>Last timestamp of today
<?php
$last_timestamp_of_today = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
echo date('Y-m-d H:i:s', $last_timestamp_of_today); //2010-12-15 23:59:59
?>First timestamp in this month
<?php
$first_day_of_month = mktime(0, 0, 0, date('m'), 1, date('Y'));
//First date of month
$FirstDayofMonth = date('Y-m-01', strtotime("20110818"));
?>Last timestamp in this month
<?php
$last_day_of_month = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
?>Get the first and last timestamp of any given timestamp or if non given use today
<?php
if ($timestamp == 0) {
$timestamp = time();
}
$first_timestamp_of_today = mktime(0, 0, 0, date('m', $timestamp), date('d', $timestamp), date('Y', $timestamp));
$last_timestamp_of_today = mktime(23, 59, 59, date('m', $timestamp), date('d', $timestamp), date('Y', $timestamp));
?>Last Saturday and coming Friday, for Friday summary of week.
<?php
//Saturday to Friday
$saturday = mktime(0, 0, 0, date('m'), date('d')-date('N')-1, date('Y'));
$friday = mktime(23, 59, 59, date('m'), date('d')+(5-date('N')), date('Y'));
//or Monday to Sunday
$monday = mktime(0, 0, 0, date('m'), date('d')-date('N')+1, date('Y'));
$sunday = mktime(23, 59, 59, date('m'), date('d')+(7-date('N')), date('Y'));
//Saturday to Friday
$saturday_date = date("m/d/Y", $saturday);
$friday_date = date("m/d/Y", $friday);
//or Monday to Sunday
$monday_date = date("m/d/Y", $monday );
$sunday_date = date("m/d/Y", $sunday);
?>And just some code...
<?php
$prevMonth = date('Y-m-t', strtotime('-1 month'));
$endTimestamp = (int) empty($date) ? strtotime($prevMonth) : strtotime(date('Y-m-t', strtotime($date.'-01')));
$startTimestamp = mktime(0, 0, 0, date("n", $endTimestamp)-5, 1, date("Y", $endTimestamp), $endTimestamp);
$prevMonth = mktime(23, 59, 59, date("m")-1, 1, date("Y"));
$prevMonth = strtotime(date("Y-m-t H:i:s", $prevMonth));
$endTimestamp = (int) empty($date) ? $prevMonth : strtotime(date('Y-m-t 23:59:00', strtotime($date.'-01')));
$startTimestamp = mktime(0, 0, 0, date("n", $endTimestamp)-5, 1, date("Y", $endTimestamp), $endTimestamp);
$now = time();
$prevMonthTime = strtotime('-1 month', $now);
$defaultMonth = date('Y-m', $prevMonthTime);
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
$day = "20110817";
$day = date("Ymd", strtotime($day . " +1 day")); //20110818
This morning:
echo $time = strtotime("now noon 4 hours ago");
echo "<br>". date("Y-m-d H:i:s", $time);
This noon:
echo $time = strtotime("now noon");
echo "<br>". date("Y-m-d H:i:s", $time);
This afternoon:
echo $time = strtotime("now noon +4 hours");
echo "<br>". date("Y-m-d H:i:s", $time);
'yesterday', 'midnight', 'back of' hour, 'front of' hour, 'first day' ' of', 'last day' ' of', 'first day of last month', 'midnight first day of last monty'
?>Get timestamp from a number of days back in time
<?php
/**
* Get timestamp from a number of days back in time
*
* @param unknown_type $check_older_than
* @return unknown
*/
function _check_older_than($check_older_than = FALSE) {
if (!$check_older_than) {
$check_older_than = variable_get('linkchecker_check_older_than', 14);
}
$this_is_old_enough = mktime(date("H"), date("i"), date("s"), date("m"), date("d")-$check_older_than, date("Y"));
return $this_is_old_enough;
}
?>Get a series of timestamps and dates starting from now and 6 month backwards using the moth last timestamp as a key.
<?php
$maxPreviousMonths = 6;
$index = 0;
$previousMonths = array();
$defaultTimestamp = null;
while($index < $maxPreviousMonths) {
$prevMonthTheFirst = mktime(00, 00, 00, date("m")-$index, 1, date("Y"));
$prevMonthTheLast = mktime(23, 59, 59, date("m", $prevMonthTheFirst), date("t", $prevMonthTheFirst), date("Y", $prevMonthTheFirst));
$previousMonthName = date('F, Y', $prevMonthTheLast);
$previousMonths[$prevMonthTheLast] = $previousMonthName;
$index++;
}
?>
<?php
/**
* Return week by date alt. add weeks to date
*
*/
function week_by_date($date, $add_weeks = "0"){
return date('W', strtotime(date($date)." $add_weeks weeks"));
}
/**
* Get starting date from week number and year
* Monday is first day of week
*
*/
function week_start_date($week, $year, $format = 'Ymd') {
$week = sprintf("%02s", $week);
$desiredMonday = date($format, strtotime("$year-W$week-1"));
return $monday;
}
?>String to time reference: http://www.php.net/manual/en/datetime.formats.relative.php
From unix timestamp to date
<?php
php > $unixTime = time();
php > print_r(getdate($unixTime));
Array
(
[seconds] => 48
[minutes] => 54
[hours] => 12
[mday] => 20
[wday] => 2
[mon] => 12
[year] => 2011
[yday] => 353
[weekday] => Tuesday
[month] => December
[0] => 1324403688
)
?>