{"id":2639,"date":"2020-04-01T19:11:51","date_gmt":"2020-04-01T18:11:51","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=2639"},"modified":"2025-10-06T21:49:47","modified_gmt":"2025-10-06T20:49:47","slug":"using-strings-values","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/strings\/using-strings-values","title":{"rendered":"Using Strings-Values"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Convert to int<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$Value = intval($Something);    \/\/integer value of value on success, or 0 on failure. Empty arrays return 0, non-empty arrays return 1.<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Formatting Numeric Values<br><\/h4>\n\n\n\n<p>Leading Zeros<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Format to 8 digits with leading zeros if necessary\n  $MyString = sprintf('%08d', 123456);<\/code><\/pre>\n\n\n\n<p>Decimal Places<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Force to 2 decimal places (outputs as a string, so \"6.30\")\n  $MyString = number_format(\"6.3\",2);  \/\/Will display 6.30\n\n\/\/Force to 2 decimal places (outputs as a number, so 6.30)\n  $MyValue = round(\"6.3456\",2);  \/\/Will output 6.30<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Convert Bool<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$intValue = $boolValue ? 1 : 0;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Displaying Date And Time<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Current Date and Time<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>   echo date(\"Y-m-d H:i:s\");\n\n   echo date(\"l dS of F Y h:i:s A\");<\/code><\/pre>\n\n\n\n<p>Values you use: <a rel=\"noreferrer noopener\" href=\"https:\/\/php.net\/manual\/en\/function.date.php\" target=\"_blank\">click here<\/a><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Get hours now<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\t$TimeNowHours = date('H');<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">From A String Variable (e.g. a MySQL result)<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $LastCheckIn = strtotime($result1&#91;'LastCheckIn']);\n\n  $LastCheckIn = date(\"Y-m-d H:i:s\", strtotime($LastCheckIn));\n\n  echo date(\"j M g:i a\", strtotime($result1&#91;'received_date']));\t<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Display as UTC<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$DisplayDate = date(\"Y-m-d H:i:s\", strtotime($DateTimeValue));  <\/code><\/pre>\n\n\n\n<p>Values you use: <a rel=\"noreferrer noopener\" href=\"https:\/\/php.net\/manual\/en\/function.date.php\" target=\"_blank\">click here<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Convert String to DateTime<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyDateTime = strtotime($MyDateTimeString);\n\n  if (($MyDateTime = strtotime($MyDateTimeString)) !== False)\n    \/\/Conversion was sucessful<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Display Value As Hex<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\techo 'My value 0x' . sprintf(\"%02X\", $MyValue);\t\t\/\/Display with 2 hex characters (or more if needed to display it)\n\techo 'My value 0x' . sprintf(\"%08X\", $MyValue);\t\t\/\/Display with 8 hex characters (or more if needed to display it)\n<\/code><\/pre>\n\n\n\n<p>Or the simpler dechex() method but without forcing the number of characters:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\techo \"&#91;\" . dechex($FileByteArray&#91;1]) . \"]\";\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Variables and array values in a string<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> echo \"His name is $name\";\n echo \"His name is ${name}y\";  \/\/&lt;&lt;Curley braces can be used if you need to force PHP to see a variable name within other characters\n echo \"His name is ${MyArray&#91;'name']}y\";\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Convert to int Formatting Numeric Values Leading Zeros Decimal Places Convert Bool Displaying Date And Time Current Date and Time Values you use: click here Get hours now From A String Variable (e.g. a MySQL result) Display as UTC Values you use: click here Convert String to DateTime Display Value As Hex Or the simpler [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-2639","post","type-post","status-publish","format-standard","hentry","category-strings"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=2639"}],"version-history":[{"count":17,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2639\/revisions"}],"predecessor-version":[{"id":5156,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2639\/revisions\/5156"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=2639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=2639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=2639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}