Dealing Date/Time

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

date_default_timezone_set('America/New_York');

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_default_timezone_set('America/New_York');
	
$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) {
		$result="
		<p>
		$that_date is $delta day(s) from today
		</p>
		" ;
		
	echo $result;
    } else {
	$result="
		<p>
		$that_date was $delta day(s) ago
		</p>
		" ;
	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
	$trim_spaces=trim($non_ascii);
	
	$final=$trim_spaces;
	
	if ($option==1){
		$final=preg_replace('/\s/', '_',$trim_spaces);
	}
	if ($option==2){
		$final=preg_replace('/\s/', '',$trim_spaces);
	}
	if ($option==3){
		
		$final=str_replace('_','',$trim_spaces);
		$final=preg_replace('/\s/', '',$final);
		$final=strtolower($final);
		
	}
	return	$final;

	}