Timezones and timestamp SQL to PHP

Dealing with godaddy mySQL timestamps on a shared server. As of this posting, although Arizona is in the Mountain time zone, it does not participate in the Daylight Savings party. Issue is that godaddy timstamps are “readable” but are in US/Arizona timezone. If the server is on a shared host, the default timezone can NOT be changed.

Here is my solution to the problem of querying a godaddy sql server timestamp:

  1. In your PHP script include the line (add your desired time zone as needed):
  2. In the $sql “SELECT….” statement, use the following:
  3. To display the time in a readable format use “date(‘format’,timestamp)” formula:
    echo date(‘D, M j – g:ia’,timestamp);

Create Post Automatically

This will create a Post using PHP.

     Code SOURCE: https://www.youtube.com/watch?v=bVImxRBKBuc
*/ //set timezone date_default_timezone_set('America/New_York'); //require native WordPress function require_once("wp-load.php"); // Post variables $userID = get_current_user_id(); $leadTitle='Operation Test Post' $leadContent='Hello there. this is the post!'; //ID# automatically assigned to categories. For multiple // categories, separate with a comma $categoryID='28'; //Time variables - Sets time for now so post can be published without delay $timeStamp = date_create(); $postdate = date("Y-m-d H:i:s", $timeStamp); //wordpress Array and Variables for posting $new_post = array( 'guid'=>$postID, 'post_title'=>$leadTitle, 'post_content'=>$leadContent, 'post_status'=>'publish', 'post_date'=>$postdate, 'post_author'=>$userID, 'post_type'=>'post', 'post_category'=>array($categoryID), 'comment_status'=>'open', ); //THIS submits the array data to creat the new post $post_id = wp_insert_post($new_post); //Error Checking if ($post_id){ echo "SUCCESS!"; } else { echo 'OOPS! something failed, please try again!'; } ?>

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;;

Disable WordPress Nickname

Place the following code in your function.php file. (Appearance > Editor > Theme Functions – functions.php)

// remove nickname
function prefix_hide_personal_options() {
        if (current_user_can('manage_options')) return false;
<script type="text/javascript">
  jQuery(document).ready(function( $ ){
if (is_admin()) add_action('personal_options', 'prefix_hide_personal_options');

Source: https://premium.wpmudev.org/blog/how-to-restrict-usernames-and-disable-nicknames-in-wordpress/

Why use “if (!function_exists(‘…”

Checking to see if built in WordPress functions exist before calling them is for backward compatibility which IMHO is not needed.

So if you see if ( function_exists( 'register_nav_menus' ) ) the theme author is supporting versions earlier than 3.0.

You still sometimes see
 if ( function_exists( 'dynamic_sidebar' ) ) 
Why? I couldn’t tell you because dynamic_sidebar was introduced in 2.2.

Another reason to use it is to make your theme or plugin pluggable. A pluggable function is one that can be overridden in a child theme or another plugin.

This is done on the definition not the call and you use the ! operator to make sure it doesn’t already exist before you define it.

if ( ! function_exists( 'my_awesome_function' ) ) {
 * My Awesome function is awesome
 * @param array $args
 * @return array
function my_awesome_function( $args ) {
  //function stuff
  return array();

When this is done a child theme or other plugin can override that function with there own.

source: https://wordpress.stackexchange.com/a/111318

WordPress Form Receive (POST)

This is the receiving page for the simple form submit.

This is the code

	if (isset($_POST['firstname']) && $_POST['firstname']){

		echo "<p>First Name: $first_name </p>";
		echo "<p>Last Name: $last_name </p>";
	} else {
		echo "<p> Error: some or all of the fields were not completed.</p>";

WordPress Form Submit (POST)

I can’t explain why this was so difficult, but it kicked my butt for weeks. I needed to create a simple form and then submit to a new page.

This is the simple form:

This is the snippet

// code for the form
	$form_html = '
		<style type="text/css">
			 .label { font-size: 100%;
				     font-size: large;
				     font-family: Arial, Helvetica, sans-serif;
					 text-decoration: underline;
			 .content { margin-left: 60px;
			 		font-size: x-large;
					font-family: Arial, Helvetica, sans-serif;
			 .button {font-size: x-large;
					font-family: Arial, Helvetica, sans-serif;
					position: relative;
  					left: 50%;
  					transform: translateX(-50%); 

		<form method="post" action="/wordpress-form-receive/" >
			 <p class="label">First Name:</p>
  				<input type="text" name="firstname" value="Mickey"><br>
  			<p class="label">Last Name:</p>
  				<input type="text" name="lastname" value="Mouse"><br><br>
		<input type="submit" class="button" value="Submit">
		</form>	';
	echo $form_html;

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;


mySQL database connection

This is how *I* connect to mySQL database(s). CPanel & Godaddy.

Step 1: create the connection string

// Create connection	
	$servername ="localhost";

	$conn = new mysqli($servername, $username, $password, $dbname);

Step 2: Check for a connection; die if not working

// Check connection
	if ($conn->connect_error) {	
		die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);

Step 3: build the SQL query. Note: the “\n” is the php line break.

$sql_query = "SELECT table.column,table.column,table.column,  \n"
	   . "FROM table \n"
	   . "WHERE table.column=conditiom \n"
	   . "ORDER By table.column";

Step 4: run the query

$result = $conn->query($sql_query);

Step 5: loop through the results

while($row = $result->fetch_assoc()) {
	$temp_variable = $row['column'];
        //do work with $temp_variable

Step 6: close out the connection



Quick code that checks to see if current user is logged in. Anything other than “0” is logged in.

//Grab the current user details from WordPress
	$current_user = wp_get_current_user();	
	if ( 0 == $current_user->ID ) {
    	echo 'Sorry.  You need to be logged in to see this information....';