Skip to content

Instantly share code, notes, and snippets.

View ryanmerritt's full-sized avatar

Ryan Merritt ryanmerritt

View GitHub Profile
@ryanmerritt
ryanmerritt / Years between two dates
Created September 18, 2012 10:16 — forked from Victa/Years between two dates
Get the number of years between two dates using PHP DateTime class
// My Birthday :)
$date_1 = new DateTime( '1989-06-15' );
// Todays date
$date_2 = new DateTime( date( 'Y-m-d' ) );
$difference = $date_2->diff( $date_1 );
// Echo the as string to display in browser for testing
echo (string)$difference->y;
@ryanmerritt
ryanmerritt / file.php
Created September 18, 2012 10:16 — forked from ravidsrk/file.php
PHP Date difference in Days
<?php
function dateDiff($start, $end) {
$start_ts = strtotime($start);
$end_ts = strtotime($end);
$diff = $end_ts - $start_ts;
return round($diff / 86400);
}
@ryanmerritt
ryanmerritt / datedifference.php
Created September 18, 2012 10:16 — forked from krishnakummar/datedifference.php
Humanize date differences in PHP (like facebook - Eg: 2 days ago, 14 hours ago, few seconds ago).
#!/usr/bin/env php
# This function prints the difference between two php datetime objects
# in a more human readable form
# inputs should be like strtotime($date)
# Adapted from https://gist.github.com/207624 python version
function humanizeDateDiffference($now,$otherDate=null,$offset=null){
if($otherDate != null){
$offset = $now - $otherDate;
}
@ryanmerritt
ryanmerritt / time.php
Created September 18, 2012 10:15 — forked from cam-gists/time.php
PHP: Unix Time Methods
<?php
print time();
//1324402770
$unixTime = time();
print_r(getdate($unixTime));
// Array
// (
// [seconds] => 48
// [minutes] => 54
// [hours] => 12
@ryanmerritt
ryanmerritt / gist:3742424
Created September 18, 2012 10:15 — forked from zachstronaut/gist:1184831
Get a relative time string in PHP without a lot of if/else or switch/case
<?php
/**
* time_elapsed_string()
*
* Zachary Johnson
* http://www.zachstronaut.com/posts/2009/01/20/php-relative-date-time-string.html
*/
function time_elapsed_string($ptime) {
$etime = time() - $ptime;
@ryanmerritt
ryanmerritt / relative_time.php
Created September 18, 2012 10:12 — forked from rowanmanning/relative_time.php
Calculate the amount of time between two dates/timestamps in PHP. Return the result as a nicely formatted string or an array of data.
<?php
/**
* Get the relative time between two timestamps.
*
* @access public
* @param integer|string $time The date or timestamp that represents the time we're comparing to.
* @param integer|string $comparison_time [optional] The date or timestamp we want comparisons to be relative to. Defaults to the current timestamp.
* @param boolean $return_array [optional] Whether to return the relative time as an array of components rather than a string. Default value is FALSE.
* @return string Returns the amount of time between the two timestamps as a string or an array if $return_array is TRUE.
@ryanmerritt
ryanmerritt / get_weekth.php
Created September 18, 2012 10:10 — forked from faisalman/get_weekth.php
Get ISO-8601 week number for a given date in PHP
<?php
/**
* Get ISO-8601 week number for a given date
*
* @author Faisalman <[email protected]>
* @link http://gist.github.com/faisalman
* @link http://en.wikipedia.org/wiki/ISO_week_date
* @param $y int year
* @param $m int month
* @param $d int day