Advertisement
johnmahugu

PHP >>> Truncate a string to the word closest to...

May 6th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.76 KB | None | 0 0
  1. <?php
  2.  
  3. // Truncate a string to the word closest to a certain number of characters
  4. preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $characters_number));
  5.  
  6. ===============================================================================================
  7.  
  8. //Compare Dates (only date, not time) :
  9.  
  10. // actual day
  11. $d_now = new \DateTime('NOW');
  12. $d_otherDate = \DateTime::createFromFormat('Y-m-d', '2013-07-14');
  13.  
  14. // do whatever with date, here add 3 months
  15. $d_otherDate->add(new \DateInterval('3PM'));
  16.  
  17. // compares the date, here get the number of days between the 2 dates
  18. $s_diff = $d_otherDate->diff($d_now)->format("%a");
  19.  
  20. if ($s_diff == '5') {
  21.   // Do Stuff...
  22. }
  23.  
  24. ===============================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement