瀏覽代碼

Add files via upload

Daniele Bonini (皮夕) | 5mode.com | WebDev | Translator 3 年之前
父節點
當前提交
3038de319e
共有 3 個文件被更改,包括 732 次插入0 次删除
  1. 426 0
      functions/func.casp-datetime-polyfills.inc
  2. 78 0
      functions/func.casp-string-polyfills.inc
  3. 228 0
      index.php

+ 426 - 0
functions/func.casp-datetime-polyfills.inc

@@ -0,0 +1,426 @@
+<?php
+
+/**
+ * Copyright 2021, 2024 5 Mode
+ *
+ * This file is part of "PHP Classic ASP polyfills".
+ *
+ * "PHP Classic ASP polyfills" is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Http Console is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.  
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Http Console. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * func.casp-string-polyfills.inc
+ * 
+ * PHP-Classic-ASP-polyfills string functions file.
+ *
+ * @author Daniele Bonini <my25mb@aol.com>
+ * @copyrights (c) 2021, 2024, 5 Mode     
+
+ //Classic Asp
+ * function is_date(string $date) {}
+ * function year(?string $date) {}
+ * function month(?string $date) {}
+ * function day(?string $date) {}
+ * function hour(?string $date) {}
+ * function minute(?string $date) {}
+ * function second(?string $date) {}
+ * function monthName(?string $date) {}
+ * function weekday(?string $date) {}
+ * function now() {}
+
+ //other stuff simplified:
+ * function date_add1() {}
+ *
+ */
+
+
+/**
+ * Check if the given string is a valid date 
+ * 
+ * @param string $string the string to check
+ * @return bool if the string is a valid date, true/false    
+ */
+if (!function_exists("is_date")) { 
+  function is_date(string $string): bool
+  {
+    $retval = date_parse($string);
+    
+    if (is_array($retval) && !empty($retval["year"])) {
+      return true;
+    } else {
+      return false;      
+    }  
+  }
+}
+
+/**
+ * Return the year of the given date or now 
+ * 
+ * @param mixed $date DateTime or string date paramenter, otherwise now 
+ * @return string the year of the given date, if the date is valid, otherwise a blank string    
+ */
+if (!function_exists("year")) { 
+  function year($date): string
+  {
+    $retval = "";
+    if (isset($date) && ($date instanceof DateTime)) {
+      $d = $date;
+      $retval=$d->format("Y");
+      $d=null;
+    } else if (isset($date) && (is_string($date) && is_date($date))) { 
+      $d = new DateTime($date);
+      $retval=$d->format("Y");
+      $d=null;
+    } else if (!isset($date)) {
+      $d = new DateTime();
+      $retval=$d->format("Y");
+      $d=null;
+    } else {
+      // any other case than..      
+    }
+    return $retval;    
+  }
+}
+
+/**
+ * Return the month of the given date or now 
+ * 
+ * @param mixed $date DateTime or string date paramenter, otherwise now 
+ * @return string the month of the given date, if the date is valid, otherwise a blank string    
+ */
+if (!function_exists("month")) { 
+  function month($date): string
+  {
+    $retval = "";
+    if (isset($date) && ($date instanceof DateTime)) {
+      $d = $date;
+      $retval=$d->format("n");
+      $d=null;
+    } else if (isset($date) && (is_string($date) && is_date($date))) { 
+      $d = new DateTime($date);
+      $retval=$d->format("n");
+      $d=null;
+    } else if (!isset($date)) {
+      $d = new DateTime();
+      $retval=$d->format("n");
+      $d=null;
+    } else {
+      // any other case than..      
+    }
+    return $retval;    
+  }
+}
+
+/**
+ * Return the day of the given date or now 
+ * 
+ * @param mixed $date DateTime or string date paramenter, otherwise now 
+ * @return string the day of the given date, if the date is valid, otherwise a blank string    
+ */
+if (!function_exists("day")) {
+  function day($date): string
+  {
+    $retval = "";
+    if (isset($date) && ($date instanceof DateTime)) {
+      $d = $date;
+      $retval=$d->format("j");
+      $d=null;
+    } else if (isset($date) && (is_string($date) && is_date($date))) { 
+      $d = new DateTime($date);
+      $retval=$d->format("j");
+      $d=null;
+    } else if (!isset($date)) {
+      $d = new DateTime();
+      $retval=$d->format("j");
+      $d=null;
+    } else {
+      // any other case than..      
+    }
+    return $retval;    
+  }
+}
+
+/**
+ * Return the hour of the given date or now 
+ * 
+ * @param mixed $date DateTime or string date paramenter, otherwise now 
+ * @return string the hour of the given date, if the date is valid, otherwise a blank string    
+ */
+if (!function_exists("hour")) {
+  function hour($date): string
+  {
+    $retval = "";
+    if (isset($date) && ($date instanceof DateTime)) {
+      $d = $date;
+      $retval=$d->format("G");
+      $d=null;
+    } else if (isset($date) && (is_string($date) && is_date($date))) { 
+      $d = new DateTime($date);
+      $retval=$d->format("G");
+      $d=null;
+    } else if (!isset($date)) {
+      $d = new DateTime();
+      $retval=$d->format("G");
+      $d=null;
+    } else {
+      // any other case than..      
+    }
+    return $retval;    
+  }
+}
+
+/**
+ * Return the minutes of the given date or now 
+ * 
+ * @param mixed $date DateTime or string date paramenter, otherwise now 
+ * @return string the minutes of the given date, if the date is valid, otherwise a blank string    
+ */
+if (!function_exists("minute")) {
+  function minute($date): string
+  {
+    $retval = "";
+    if (isset($date) && ($date instanceof DateTime)) {
+      $d = $date;
+      $retval=$d->format("i");
+      $d=null;
+    } else if (isset($date) && (is_string($date) && is_date($date))) { 
+      $d = new DateTime($date);
+      $retval=$d->format("i");
+      $d=null;
+    } else if (!isset($date)) {
+      $d = new DateTime();
+      $retval=$d->format("i");
+      $d=null;
+    } else {
+      // any other case than..      
+    }
+    if (substr($retval,0,1)==="0") {
+      $retval = substr($retval,1);
+    }  
+    return $retval;    
+  }
+}
+
+/**
+ * Return the seconds of the given date or now 
+ * 
+ * @param mixed $date DateTime or string date paramenter, otherwise now 
+ * @return string the seconds of the given date, if the date is valid, otherwise a blank string    
+ */
+if (!function_exists("second")) {
+  function second($date): string
+  {
+    $retval = "";
+    if (isset($date) && ($date instanceof DateTime)) {
+      $d = $date;
+      $retval=$d->format("s");
+      $d=null;
+    } else if (isset($date) && (is_string($date) && is_date($date))) { 
+      $d = new DateTime($date);
+      $retval=$d->format("s");
+      $d=null;
+    } else if (!isset($date)) {
+      $d = new DateTime();
+      $retval=$d->format("s");
+      $d=null;
+    } else {
+      // any other case than..      
+    }
+    if (substr($retval,0,1)==="0") {
+      $retval = substr($retval,1);
+    }  
+    return $retval;    
+  }
+}
+
+/**
+ * Return the month name of the given date or now 
+ * 
+ * @param mixed $date DateTime or string date paramenter, otherwise now 
+ * @return string the month name of the given date, if the date is valid, otherwise a blank string    
+ */
+if (!function_exists("monthName")) {
+  function monthName($date): string
+  {
+    $retval = "";
+    if (isset($date) && ($date instanceof DateTime)) {
+      $d = $date;
+      $retval=$d->format("F");
+      $d=null;
+    } else if (isset($date) && (is_string($date) && is_date($date))) { 
+      $d = new DateTime($date);
+      $retval=$d->format("F");
+      $d=null;
+    } else if (!isset($date)) {
+      $d = new DateTime();
+      $retval=$d->format("F");
+      $d=null;
+    } else {
+      // any other case than..      
+    }
+    return $retval;    
+  }
+}
+
+/**
+ * Return the week day of the given date or now 
+ * 
+ * @param mixed $date DateTime or string date paramenter, otherwise now 
+ * @return string the week day of the given date, if the date is valid, otherwise a blank string    
+ */
+if (!function_exists("weekday")) {
+  function weekday($date): string
+  {
+    $retval = "";
+    if (isset($date) && ($date instanceof DateTime)) {
+      $d = $date;
+      $retval=$d->format("l");
+      $d=null;
+    } else if (isset($date) && (is_string($date) && is_date($date))) { 
+      $d = new DateTime($date);
+      $retval=$d->format("l");
+      $d=null;
+    } else if (!isset($date)) {
+      $d = new DateTime();
+      $retval=$d->format("l");
+      $d=null;
+    } else {
+      // any other case than..      
+    }
+    return $retval;    
+  }
+}
+
+/**
+ * Return the current date 
+ * 
+ * @return DateTime the current date
+ */
+if (!function_exists("now")) {
+  function now(): string
+  {
+      $d = new DateTime();
+      $retval=$d->format("Y/d/m H:i:s A");
+      $d=null;
+      return $retval;  
+  }
+}
+
+/**
+ * Add the specified interval to the given date
+ *
+ * @param string $intervalType the type of interval to add to the string
+ *               it can be one of the followin values: 
+ *
+ *               Y - Year
+ *               m - Month
+ *               W - Weeks 
+ *               d - Day
+ *               H - Hour
+ *               i - Minute
+ *               s - Second
+ *
+ * @param int $interval the interval to add (or subtract if negtive)
+ * @param mixed $date the date to add the interval to
+ * 
+ * @return the resulting date otherwise false in case of error
+ */
+if (!function_exists("date_add1")) {
+  function date_add1(string $intervalType, int $interval, $date)
+  {
+     $retval = false;
+     
+     if (!isset($intervalType) || !isset($interval)) {
+       return $retval;
+     }
+     
+     if ($interval === 0) {
+       return $retval;
+     }
+     
+     // Parsing $date
+     if (isset($date) && ($date instanceof DateTime)) {
+       $d = $date;
+     } else if (isset($date) && (is_string($date) && is_date($date))) { 
+       $d = new DateTime($date);
+     } else if (!isset($date)) {
+       $d = new DateTime();
+     } else {
+       // any other case than..
+       return $retval;      
+     }   
+     
+     if ($interval > 0) {
+     
+       switch($intervalType)
+       {
+         case "Y":
+           date_add($date, date_interval_create_from_date_string($interval . ' years'));
+           break;
+         case "m":
+           date_add($date, date_interval_create_from_date_string($interval . ' months'));
+           break;
+         case "W":
+           $days = $interval * 7;
+           date_add($date, date_interval_create_from_date_string($interval . ' days'));
+           break;
+         case "d":
+           date_add($date, date_interval_create_from_date_string($interval . ' days'));
+           break;
+         case "H":
+           date_add($date, date_interval_create_from_date_string($interval . ' hours'));
+           break;
+         case "i":
+           date_add($date, date_interval_create_from_date_string($interval . ' minutes'));
+           break;
+         case "s":
+           date_add($date, date_interval_create_from_date_string($interval . ' seconds'));
+           break;
+         default:
+           return $retval;  
+       }
+
+     } else { 
+
+       switch($intervalType)
+       {
+         case "Y":
+           date_sub($date, date_interval_create_from_date_string($interval . ' years'));
+           break;
+         case "m":
+           date_sub($date, date_interval_create_from_date_string($interval . ' months'));
+           break;
+         case "W":
+           $days = $interval * 7;
+           date_sub($date, date_interval_create_from_date_string($interval . ' days'));
+           break;
+         case "d":
+           date_sub($date, date_interval_create_from_date_string($interval . ' days'));
+           break;
+         case "H":
+           date_sub($date, date_interval_create_from_date_string($interval . ' hours'));
+           break;
+         case "i":
+           date_sub($date, date_interval_create_from_date_string($interval . ' minutes'));
+           break;
+         case "s":
+           date_sub($date, date_interval_create_from_date_string($interval . ' seconds'));
+           break;
+         default:
+           return $retval;  
+       }
+
+     }
+     
+     return $date;
+  }
+}

+ 78 - 0
functions/func.casp-string-polyfills.inc

@@ -0,0 +1,78 @@
+<?php
+
+/**
+ * Copyright 2021, 2024 5 Mode
+ *
+ * This file is part of "PHP Classic ASP polyfills".
+ *
+ * "PHP Classic ASP polyfills" is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Http Console is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.  
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Http Console. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * func.casp-string-polyfills.inc
+ * 
+ * PHP-Classic-ASP-polyfills string functions file.
+ *
+ * @author Daniele Bonini <my25mb@aol.com>
+ * @copyrights (c) 2021, 2024, 5 Mode     
+ */
+
+/**
+ * Left cut the given substring for the specified length 
+ * 
+ * @param string $string the string being left cut
+ * @param int $length the length of the substring to return
+ * @return string the resulting substring    
+ */
+if (!function_exists("left")) {
+  function left(string $string, int $length): string 
+  {
+    if (!isset($string) || $string === "") {
+      return "";
+    }
+    return mb_substr($string, 0, $length);
+  }
+}
+
+/**
+ * Right cut the given string for the specified length 
+ * 
+ * @param string $string the string being right cut
+ * @param int $length the length of the substring to return
+ * @return string the resulting substring    
+ */
+if (!function_exists("right")) {   
+  function right(string $string, int $length): string 
+  {
+    if (!isset($string) || $string === "") {
+      return "";
+    }  
+    return mb_substr($string, mb_strlen($string) - $length);
+  }
+}
+
+/**
+ * Repeat space $num times
+ *
+ * @param int $num numer of times space should be repeated
+ * 
+ * @return string space repeated $num times 
+ */
+if (!function_exists("space")) {   
+  function space(int $num): string
+  {
+    if ($num === 0) {
+      return "";
+    }
+    return str_repeat(" ", $num); 
+  }
+}

+ 228 - 0
index.php

@@ -0,0 +1,228 @@
+<?php
+
+/**
+ * Copyright 2021, 2024 5 Mode
+ *
+ * This file is part of "PHP Classic ASP polyfills".
+ *
+ * "PHP Classic ASP polyfills" is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Http Console is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.  
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Http Console. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * index.php
+ * 
+ * PHP-Classic-ASP-polyfills index file.
+ *
+ * @author Daniele Bonini <my25mb@aol.com>
+ * @copyrights (c) 2021, 2024, 5 Mode     
+ */
+
+define("FUNCTIONS_PATH", __DIR__.DIRECTORY_SEPARATOR."functions");
+
+// CLASSIC ASP POLYFILLS
+require FUNCTIONS_PATH . DIRECTORY_SEPARATOR . "/func.casp-datetime-polyfills.inc";
+require FUNCTIONS_PATH . DIRECTORY_SEPARATOR . "/func.casp-string-polyfills.inc";
+
+?>
+
+<pre style="width:500px">
+  // left() example
+  $s = "Reputation is everything. Almost everything..";
+  echo(left($s,10)."&lt;br&gt;");
+</pre>
+
+<b>Result:</b><br>
+<?php
+  $s = "Reputation is everything. Almost everything..";
+  echo(left($s,10)."<br><br>");
+?> 
+
+<pre style="width:500px">
+  // right() example
+  $s = "Reputation is everything. Almost everything..";
+  echo(right($s,19)."&lt;br&gt;");
+</pre>
+
+<b>Result:</b><br>
+<?php 
+  $s = "Reputation is everything. Almost everything..";
+  echo(right($s,19)."<br><br>");
+?> 
+
+<pre style="width:500px">
+  // space() example
+  $s = "Reputation is everything. Almost everything..";
+  echo(left($s,10).space(5).right($s,35)."&lt;br&gt;");
+</pre>
+
+<b>Result:</b><br>
+<?php 
+  $s = "Reputation is everything. Almost everything..";
+  echo(left($s,10).space(5).right($s,35)."<br><br>");
+?> 
+
+<pre style="width:500px">
+  // id_date() example #1
+  $s = "Reputation is everything. Almost everything..";
+  echo((is_date($s)?"true":"false")."&lt;br&gt;");
+</pre>
+
+<b>Result:</b><br>
+<?php 
+  $s = "Reputation is everything. Almost everything..";
+  echo((is_date($s)?"true":"false")."<br><br>");
+?> 
+
+<pre style="width:500px">
+  // id_date() example #2
+  $s = "27/09/2021";
+  echo((is_date($s)?"true":"false")."&lt;br&gt;");
+</pre>
+
+<b>Result:</b><br>
+<?php 
+  $s = "27/09/2021";
+  echo((is_date($s)?"true":"false")."<br><br>");
+?> 
+
+<pre style="width:500px">
+  // year() example #1
+  $v = "27/09/2021";
+  echo(year($v)."&lt;br&gt;");
+</pre>
+
+<b>Result:</b><br>
+<?php 
+  $v = "09/27/2021";
+  echo(year($v)."<br><br>");
+?> 
+
+<pre style="width:500px">
+  // year() example #2
+  $v = new DateTime("09/27/2021");
+  echo(year($v)."&lt;br&gt;");
+</pre>
+
+<b>Result:</b><br>
+<?php 
+  $v = new DateTime("09/27/2021");
+  echo(year($v)."<br><br>");
+?> 
+
+<pre style="width:500px">
+  // month() example
+  $v = new DateTime("09/27/2021");
+  echo(month($v)."&lt;br&gt;");
+</pre>
+
+<b>Result:</b><br>
+<?php 
+  $v = new DateTime("09/27/2021");
+  echo(month($v)."<br><br>");
+?> 
+
+<pre style="width:500px">
+  // day() example
+  $v = new DateTime("09/27/2021");
+  echo(day($v)."&lt;br&gt;");
+</pre>
+
+<b>Result:</b><br>
+<?php 
+  $v = new DateTime("09/27/2021");
+  echo(day($v)."<br><br>");
+?> 
+
+<pre style="width:500px">
+  // hour() example
+  $v = new DateTime("09/27/2021 22:05:55");
+  echo(hour($v)."&lt;br&gt;");
+</pre>
+
+<b>Result:</b><br>
+<?php 
+  $v = new DateTime("09/27/2021 22:05:55");
+  echo(hour($v)."<br><br>");
+?> 
+
+<pre style="width:500px">
+  // minute() example
+  $v = new DateTime("09/27/2021 22:05:55");
+  echo(minute($v)."&lt;br&gt;");
+</pre>
+
+<b>Result:</b><br>
+<?php 
+  $v = new DateTime("09/27/2021 22:05:55");
+  echo(minute($v)."<br><br>");
+?> 
+
+<pre style="width:500px">
+  // second() example
+  $v = new DateTime("09/27/2021 22:05:55");
+  echo(second($v)."&lt;br&gt;");
+</pre>
+
+<b>Result:</b><br>
+<?php 
+  $v = new DateTime("09/27/2021 22:05:55");
+  echo(second($v)."<br><br>");
+?> 
+
+<pre style="width:500px">
+  // monthName() example
+  $v = new DateTime("09/27/2021 22:05:55");
+  echo(monthName($v)."&lt;br&gt;");
+</pre>
+
+<b>Result:</b><br>
+<?php 
+  $v = new DateTime("09/27/2021 22:05:55");
+  echo(monthName($v)."<br><br>");
+?> 
+
+<pre style="width:500px">
+  // weekday() example
+  $v = new DateTime("09/27/2021 22:05:55");
+  echo(weekday($v)."&lt;br&gt;");
+</pre>
+
+<b>Result:</b><br>
+<?php 
+  $v = new DateTime("09/27/2021 22:05:55");
+  echo(weekday($v)."<br><br>");
+?> 
+
+<pre style="width:500px">
+  // now() example
+  echo(now()."&lt;br&gt;");
+</pre>
+
+<b>Result:</b><br>
+<?php 
+  echo(now()."<br><br>");
+?> 
+
+<pre style="width:500px">
+  // date_add1() example #1
+  $d1 = new DateTime("09/27/2021 22:05:55");
+  $d1 = date_add1("Y", 1, $d1);
+  echo($d1->format("Y/d/m H:i:s A")."&lt;br&gt;");
+</pre>
+
+<b>Result:</b><br>
+<?php 
+  $d1 = new DateTime("09/27/2021 22:05:55");
+  $d1 = date_add1("Y", 1, $d1);
+  echo($d1->format("Y/d/m H:i:s A")."<br><br>");
+?> 
+