Skip to content

Instantly share code, notes, and snippets.

@ksnider
Last active January 14, 2019 18:57
Show Gist options
  • Select an option

  • Save ksnider/8487131 to your computer and use it in GitHub Desktop.

Select an option

Save ksnider/8487131 to your computer and use it in GitHub Desktop.
<?php
// Create new DateTime objects and format them
// Specified date/time in your server's time zone.
$date = date_create('2001-01-01');
echo $date->format('Y-m-d H:i:s P') . "<br/><br/>";
// Specified date/time in the specified time zone.
$date = date_create('2004-07-26', timezone_open('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:s P') . "<br/><br/>";
// Current date/time in your server's time zone.
$date = date_create();
echo $date->format('Y-m-d H:i:s A P') . "<br/><br/>";
// Current date/time in the specified time zone.
$date = date_create(null, timezone_open("America/New_York"));
echo $date->format('Y-m-d H:i:s a T') . "<br/><br/>";
// Non-existent values roll over.
$date = date_create('2000-02-30');
echo $date->format('Y-m-d H:i:s e') . "<br/><br/>";
$today = date_create('10:36 PM', timezone_open("America/New_York"));
$tomorrow = date_create('tomorrow 11:00 AM', timezone_open("America/New_York"));
echo $infuDate = date_format($today, "Y-m-d\TH:i:s"). "<br/><br/>"; // 2013-12-20T16:30:00
echo $humanDate = date_format($today, "F jS"). "<br/><br/>"; // December 20th
echo $humanTime = date_format($today, "g:i A T"). "<br/><br/>"; // returns 4:30 EST
echo $infuDate = date_format($tomorrow, "Y-m-d\TH:i:s"). "<br/><br/>"; // 2013-12-19T16:30:00
echo $myDate = date_format($tomorrow, 'Ymd'). "<br/><br/>"; // 20131219
// To change a date, use date_date_set()
$date = date_create();
echo date_format($date, 'Y-m-d') . "<br/>";
date_date_set($date, 2001, 2, 3);
echo date_format($date, 'Y-m-d') . "<br/><br/>";
//To change a time, date_time_set()
$date = date_create('2001-01-01');
echo date_format($date, 'Y-m-d H:i:s') . "<br/>";
date_time_set($date, 14, 55);
echo date_format($date, 'Y-m-d H:i:s') . "<br/><br/>";
//To change a time zone, date_time_set()
$date = date_create('2000-01-01', timezone_open('America/Anchorage'));
echo date_format($date, 'Y-m-d H:i:s T') . "<br/>";
date_timezone_set($date, timezone_open('America/Chicago'));
echo date_format($date, 'Y-m-d H:i:s T') . "<br/><br/>";
// To change using a string, use date_modify()
$date = date_create('2006-12-12');
echo date_format($date, 'Y-m-d') . "<br/>";
date_modify($date, '+1 day');
echo date_format($date, 'Y-m-d') . "<br/><br/>";
// More On Timezones
$date = date_create();
echo date_format($date, 'Y-m-d H:i:s T') . '<br/>';
date_default_timezone_set('America/New_York');
$date = date_create();
echo date_format($date, 'Y-m-d H:i:s T');
// Comparing dates
$today = date_create(null, timezone_open("America/New_York"));
$next_class = date_create('2014-01-21 16:30', timezone_open('America/New_York'));
if($today > $next_class) {
echo 'Too late!';
} else {
echo "You still have time to register!";
}
echo '<br/>';
// Using date_diff
$interval = date_diff($today, $next_class);
echo "<pre>";
print_r($interval);
echo "</pre>";
$days = date_interval_format($interval, "%a days");
echo "You still have $days to register <br/><br/>";
// Adding the diff to another DateTime
echo 'Today is ' . date_format($today, 'Y-m-d H:i:s T') . "<br/>";
echo 'Next class is ' . date_format($next_class, 'Y-m-d H:i:s T') . "<br/><br/>";
date_add($today, $interval);
echo 'Today is ' . date_format($today, 'Y-m-d H:i:s T') . "<br/>";
echo 'Next class is ' . date_format($next_class, 'Y-m-d H:i:s T') . "<br/><br/>";
date_sub($today, $interval);
echo 'Today is ' . date_format($today, 'Y-m-d H:i:s T') . "<br/>";
echo 'Next class is ' . date_format($next_class, 'Y-m-d H:i:s T') . "<br/><br/>";
// Some API Examples
require_once("isdk.php");
$app = new iSDK;
if ($app->cfgCon("sandbox")) {
// Current date in Infusionsoft-friendly format
$currentDate = date("Ymd H:i:s"); // Notice old date() function
$currentDate = date_format(date_create(), 'Ymd H:i:s');
echo "You connected at $currentDate <br/>";
echo $iDate = $app->infuDate($currentDate);
// Check to see if these fields are already set
$returnFields = array('_FirstPurchaseDate', '_HumanizedWebinarDate', 'Anniversary');
$conDat = $app->loadCon(133, $returnFields);
echo "<pre>";
print_r($conDat);
echo "</pre>";
// If not, set them
echo $webHuman = date_format(date_create('Feb 26, 2014'), 'F jS');
echo '<br/><br/>';
if(!isset($conDat['_FirstPurchaseDate'])) { // notice this is different
$update = array('_FirstPurchaseDate' => $currentDate);
echo $conID = $app->updateCon(133, $update) . "<br/>"; // !
}
if(!$conDat['_HumanizedWebinarDate']) { // than this
$update = array('_HumanizedWebinarDate' => $webHuman);
echo $conID = $app->updateCon(133, $update). "<br/>";
}
if(!$conDat['Anniversary']) {
$update = array('Anniversary' => $currentDate);
echo $conID = $app->updateCon(133, $update). "<br/>";
}
// How long between first login and first purchase
// Extra credit: Why did I have to use date_create again?
$firstLogin = date_create($conDat['Anniversary']);
$firstPurchase = date_create($conDat['_FirstPurchaseDate']);
$interval = date_diff($firstLogin, $firstPurchase);
$days = date_interval_format($interval, "%a days");
echo "Took $days for this person to purchase";
}else {
echo "Not Connected...";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment