Convert to int

$Value = intval($Something);    //integer value of value on success, or 0 on failure. Empty arrays return 0, non-empty arrays return 1.

Formatting Numeric Values

Leading Zeros

//Format to 8 digits with leading zeros if necessary
  $MyString = sprintf('%08d', 123456);

Decimal Places

//Force to 2 decimal places (outputs as a string, so "6.30")
  $MyString = number_format("6.3",2);  //Will display 6.30

//Force to 2 decimal places (outputs as a number, so 6.30)
  $MyValue = round("6.3456",2);  //Will output 6.30

Displaying Date And Time

Current Date and Time
   echo date("Y-m-d H:i:s");

   echo date("l dS of F Y h:i:s A");

Values you use: click here

Get hours now
	$TimeNowHours = date('H');
From A String Variable (e.g. a MySQL result)
  $LastCheckIn = strtotime($result1['LastCheckIn']);

  $LastCheckIn = date("Y-m-d H:i:s", strtotime($LastCheckIn));

  echo date("j M g:i a", strtotime($result1['received_date']));	
Display as UTC
$DisplayDate = date("Y-m-d H:i:s", strtotime($DateTimeValue));  

Values you use: click here

Convert String to DateTime

  $MyDateTime = strtotime($MyDateTimeString);

  if (($MyDateTime = strtotime($MyDateTimeString)) !== False)
    //Conversion was sucessful

Display Value As Hex


	echo 'My value 0x' . sprintf("%02X", $MyValue);		//Display with 2 hex characters (or more if needed to display it)
	echo 'My value 0x' . sprintf("%08X", $MyValue);		//Display with 8 hex characters (or more if needed to display it)

Or the simpler dechex() method but without forcing the number of characters:


	echo "[" . dechex($FileByteArray[1]) . "]";

Variables and array values in a string

 echo "His name is $name";
 echo "His name is ${name}y";  //<<Curley braces can be used if you need to force PHP to see a variable name within other characters
 echo "His name is ${MyArray['name']}y";

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *