Dealing Date/Time

Setting the default timezone (otherwise, everything will be UTC)


Creating a NOW timestamp as Object

$today=new DateTime();
echo $today->format('Y-m-d H:i:s');

Creating a NOW timestamp procedurally

$today = date_create();
echo date_format($today, 'Y-m-d H:i:s');

Time between two dates (not the cleanest way)

$this_date = (new DateTime()) -> getTimestamp();  //today
$other_date = strtotime('2020-07-14'); //some other date

//Result is in number of seconds, will need to be converted
$delta = $that_date-$this_date;

Quick code for time between two dates:

$date_format='F jS, Y';
$this_date = (new DateTime()) -> getTimestamp();  //today 
$that_date = strtotime("2020-07-07");  //random date mySQL format

$delta = $that_date-$this_date;
$delta = round($delta/60/60/24,1);

$that_date = date($date_format,$that_date);

if ($delta > 0) {
		$that_date is $delta day(s) from today
		" ;
	echo $result;
    } else {
		$that_date was $delta day(s) ago
		" ;
	echo $result;;

Clean Text Function

I needed a way to remove non-printing and irregular ascii character. This is helpfull when controlling user name formatting or comparing two strings. The output will be the cleaned up string based on the options.

Usage: $result=text_clean(<text to be reviewed>,<optional variable>);

	this function will strip all non-printing and 
	irregular ascii symbols and repetitive spaces.  
	Option 0: (default) leaves spaces; 
	Option 1: replaces spaces with underscores; 
	Option 2: removes all spaces.
	Option 3: removes all spaces, underscores, converts to lowercase

function text_clean($submitted,$option=0){

	//reduces multiple spaces to single
	$excess_spaces=preg_replace('/\s\s+/', ' ',$submitted);
	//removes all non_printing/extended ascii characters
	$non_ascii=preg_replace('/[^\da-z ]/i', '', $excess_spaces);

	//Truncates the leading and ending spaces
	if ($option==1){
		$final=preg_replace('/\s/', '_',$trim_spaces);
	if ($option==2){
		$final=preg_replace('/\s/', '',$trim_spaces);
	if ($option==3){
		$final=preg_replace('/\s/', '',$final);
	return	$final;