Skip to content

Instantly share code, notes, and snippets.

View ryanmerritt's full-sized avatar

Ryan Merritt ryanmerritt

View GitHub Profile
@ryanmerritt
ryanmerritt / gist:3745830
Created September 18, 2012 20:54 — forked from philsturgeon/gist:3228499
DateTime comparison (strings)
<?php
function calculateDateDiff($strStart, $strEnd) {
$objStart = DateTime::createFromFormat("m/j/Y g:i:s A", $strStart);
$objEnd = DateTime::createFromFormat("m/j/Y g:i:s A", $strEnd);
// Return Total Seconds
return $objEnd->getTimestamp() - $objStart->getTimestamp();
// Return formatted diference (7hrs 5mins and 43 seconds)
return $objStart->diff($objEnd)->format('%hhrs %imins and %sseconds ');
@ryanmerritt
ryanmerritt / gist:3745827
Created September 18, 2012 20:53 — forked from philsturgeon/gist:3228471
Complicated Compare date difference (Seconds)
<?php
function calculateDateDiff($strStart, $strEnd) {
$parts = explode(' ', $strStart);
$dates = explode('/', $parts[0]);
$times = explode(':', $parts[1]);
$unix_start = mktime($times[0], $times[1], $times[2], $dates[0], $dates[1], $dates[2]);
$parts = explode(' ', $strEnd);
$dates = explode('/', $parts[0]);
$times = explode(':', $parts[1]);
@ryanmerritt
ryanmerritt / MyDateTime.php
Created September 18, 2012 10:23 — forked from colindecarlo/MyDateTime.php
Calculate the last <blank> of the month
<?php
class MyDateTime extends DateTime
{
protected $_daysOfTheWeek = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
public function lastDayOfTheMonth()
{
@ryanmerritt
ryanmerritt / formatDateTime.php
Created September 18, 2012 10:23 — forked from nomanson/formatDateTime.php
PHP function for formatting date and time
function format_datetime($examine_query)
{
$split_query = explode("FROM", $examine_query);
$result = mysql_query($examine_query);
$numfields = mysql_num_fields($result);
for ($i = 0; $i < $numfields; $i++)
{
$field_info = mysql_field_type($result, $i);
$field_name = mysql_field_name($result, $i);
<?php
$d = new DateTime();
echo $d->format("r\n");
//OUT: Wed, 06 Jul 2011 17:31:13 -0400
$d->setTime(0, 0, 0);
$mt = clone $d;
@ryanmerritt
ryanmerritt / datetime.php
Created September 18, 2012 10:22
PHP MySQL DATETIME Conversion Code for both inserting into and displaying from a mysql db.
<?php
// DATE/TIME Manipulation for dates from php to mysql and back again
// get time and date right now for inserting datetime into mysql
// the field in the db should be set as a mysql "datetime" field type
// First - set the local timezone
date_default_timezone_set('America/New_York')
// use this in a hidden form field to insert the time into db record
echo date('Y-m-d H:i:s',time());
@ryanmerritt
ryanmerritt / datetime_timezones.php
Created September 18, 2012 10:19 — forked from sobstel/datetime_timezones.php
PHP: Converting DateTime between timezones
<?php
// now
$date = date_create('2010-12-31 22:00:01', timezone_open('Europe/Amsterdam'));
// Buenos Aires
date_timezone_set($date, timezone_open('America/Argentina/Buenos_Aires'));
echo $date->format('Y-m-d H:i:s');
// 2010-12-31 18:00:01
// UTC
@ryanmerritt
ryanmerritt / datetime.php
Created September 18, 2012 10:18 — forked from ziadoz/datetime.php
Using PHP DateTime
<?php
// Comparison and Formatting
$start = new DateTime('now');
$end = new DateTime('2014-02-01');
echo 'Start: ' . $start->format('d M Y') . "\n";
echo 'End: ' . $end->format('d M Y') . "\n";
$diff = $end->diff($start);
echo 'Comparison: ' . ($start < $end ? 'Start is less than End' : 'End is less than Start') . "\n";
@ryanmerritt
ryanmerritt / validate_date.php
Created September 18, 2012 10:17
PHP - Validate a date
/**
* Validate a date.
*
* Source: http://www.phpro.org/examples/Validate-Date-Using-PHP.html
*
* @param string $date
* @param string $format
* @return bool
*
*/
@ryanmerritt
ryanmerritt / gist:3742433
Created September 18, 2012 10:17
Verify a date string is ISO 8601 formatted in PHP
// Regex from http://www.pelagodesign.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/
function assertISO8601Date($dateStr) {
if (preg_match('/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/', $dateStr) > 0) {
return TRUE;
} else {
return FALSE;
}
}