I use human_time_diff Wordpress function on a small area in a thumbnail to show from how much time is it posted, and it appears like: 2 hours , 24 mins ...., and I was wondering if I can make it shorter, like 2h, 24m, I'm sure it could be changed somewhere, can I get some help?
WordPress has a filter available to do this:
add_filter( 'human_time_diff', function($since, $diff, $from, $to) {
$replace = array(
'min' => 'm',
'mins' => 'm',
'hour' => 'h',
'hours' => 'h',
'day' => 'd',
'days' => 'd',
);
return strtr($since, $replace);
}, 10, 4 );
Related
Hell internet world
I bring another weird and wonderful question to you today.
See the code blow, locally (using Laravel's inbuilt php serve functionality, this outputs the following:
Which is the desired result
[2019-01-31 14:33:07] local.DEBUG: >>: q
[2019-01-31 14:33:07] local.DEBUG: --: q
[2019-01-31 14:33:07] local.DEBUG: ##: 0000q
However, the same files, uploaded to an AWS t2.micro server running Ubuntu 18 LTS - i receive the following:
not desired
[2019-01-31 14:22:47] local.DEBUG: >>: 14
[2019-01-31 14:22:47] local.DEBUG: --: 14
[2019-01-31 14:22:47] local.DEBUG: ##: 00014
Here is the code snippet to generate an alpha numeric membership number:
Thanks to this previous question
<?php
namespace App\Http\Controllers;
use App\Http\Requests\RegisterFormRequest;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use JWTAuth;
use PragmaRX\Countries\Package\Countries;
use App\Mail\Welcome;
use App\cart_storage;
use Log;
class AuthController extends Controller
{
private function generateVerification($name, $title) {
return md5( $name.substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"), 0, 9).$title );
}
private function mybase33($number) {
return strtr(base_convert($number, 10, 33), [
'i' => 'j',
'j' => 'k',
'k' => 'm',
'l' => 'n',
'm' => 'p',
'n' => 'q',
'o' => 'r',
'p' => 's',
'q' => 't',
'r' => 'u',
's' => 'v',
't' => 'w',
'u' => 'x',
'v' => 'y',
'w' => 'z',
]);
}
public function registerItem(request $request){
$id = (int)Auth::user()->id;
$memberCodeShort = $this->mybase33($id);
$membershipNumber = str_pad($memberCodeShort, 5, "0", STR_PAD_LEFT);
Log::debug('>>: '.$this->mybase33($id));
//$user->membership_number = $this->generateMembershipNumber($id);
Log::debug('--: '.$memberCodeShort);
Log::debug('##: '.$membershipNumber);
}
}
Any thoughts on what to look at, i'm completely lost. But my gut is telling me there is likely to be something missing or not setup on the server correctly
Just to formalize things from the comments:
Your user ID is different on production. 🤯🤬😂
I would like to generate a membership number consisting of alphanumeric characters, removing i o and l to save confusion when typing. to be done in php (also using Laravel 5.7 if that matters - but i feel this is a php question)
If simply using 0-9 the membership number would start at 00001 for the 1st one and the 11th person would have 00011. I would like to use alphanumeric characters from 0-9 + a-z (removing said letters)
0-9 (total 10 characters), abcdefghjkmnpqrstuvwxyz (total 23 characters) - this giving a total of 33 characters in each count cycle (0-10+a-Z). instead of just 10 (0-10)
So the first membership number would still be 00001 where as the 12th would now be 0000a, 14th 0000c and 34th would be 0001a.
To summarize i need a way of defining the characters for counting in a way that can be generated based on the id of a user.
I hope I have explained this well enough.
Assuming that these are the only characters you want to use:
0123456789abcdefghjkmnpqrstuvwxyz
You can use base_convert() and strtr() to translate specific characters of the result to the characters you want.
function mybase33($number) {
return str_pad(strtr(base_convert($number, 10, 33), [
'i' => 'j',
'j' => 'k',
'k' => 'm',
'l' => 'n',
'm' => 'p',
'n' => 'q',
'o' => 'r',
'p' => 's',
'q' => 't',
'r' => 'u',
's' => 'v',
't' => 'w',
'u' => 'x',
'v' => 'y',
'w' => 'z',
]), 5, '0', STR_PAD_LEFT);
}
echo "9 is ".mybase33(9)."\n";
echo "10 is ".mybase33(10)."\n";
echo "12 is ".mybase33(12)."\n";
echo "14 is ".mybase33(14)."\n";
echo "32 is ".mybase33(32)."\n";
echo "33 is ".mybase33(33)."\n";
echo "34 is ".mybase33(34)."\n";
Output:
9 is 00009
10 is 0000a
12 is 0000c
14 is 0000e
32 is 0000z
33 is 00010
34 is 00011
https://3v4l.org/8YtaR
Explanation
The output of base_convert() uses these characters:
0123456789abcdefghijklmnopqrstuvw
The strtr() translates specific characters of that output to:
0123456789abcdefghjkmnpqrstuvwxyz
I have a rating system on my Wordpress that came with the theme. The maximum rating possible is 10, so I wanted to edit this and make a 100 possible rating.
So I edited this part:
public static function max_rating( $listing_id = null ) {
$default = 100;
So now it understands that the max possible rating is 100.
But under the rating array, there were these lines:
$rating_options = array(
'1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
'6' => 6,
'7' => 7,
'8' => 8,
'9' => 9,
'10' => 10,
Which understand that the maximum possible rating is 10. Now I want to make a maximum rating of 100, but adding '11' => 11, '12' => 12, '13' => 13 etc takes a lot of time and it consumes a lot of space in my file. Is there a possiblity to shorten this or do I really have to enter every rating up to 100?
The accepted answer points you in the right direction.
Additional I would advice using array_combine like so:
$range = range(1,100);
$rating_options = array_combine($range, $range);
// array(1=>1, 2=>2, ...)
This way, your keys will be the same as the values.
you can use PHP's range function:
$ratings = range(0, 100);
reference: https://secure.php.net/manual/en/function.range.php
[EDIT] : I guess people had problem to understand exactly what I mean, so I completely rewrote my explanations.
I work on a project where users can define a date format used in the whole site. It uses PHP date format standard. For example : "year-month-day" is set by "Y-m-d".
PHP standard uses single-character symbols like Y, m, d, F, j to describe the date format. As seen in the documentation : http://www.php.net/manual/en/function.date.php
Sometimes users can select a date thanks to a jQueryUI Datepicker. This component describes its date format with code-words like yy, y, mm, dd, D, ...
http://api.jqueryui.com/datepicker/#utility-formatDate
I would like to display the dates in the same format for both PHP and the Datepicker.
I mean that PHP should output the date as in the format set by user, AND the Datepicker should show the selected date in the same format.
Given that:
The date format is necessarily described "PHP style"
I can't know a priori which format was set by users
/!\ This not a problem of how to read/parse/display a date from a known format.
Unfortunately, Javascript date format description is not the same as in PHP.
For instance, these 2 date formats are equivalent but described differently in PHP and Javascript:
PHP : Y-m-d (set by users)
Javascript : yy-mm-dd
As you can see, I cannot just configure the datepicker with the PHP date format, because it will be misunderstood, or not recognized at all.
Someone (in answers below) adviced to create my own "date format standard converter", matching each PHP symbol with its equivalent in JS date format description. Just like:
Y => yy
m => mm
d => dd
y => y
z => o
...
And then replace each PHP symbol with the JS one. And so "d/m/Y" will be translated into "dd/mm/yy", magically.
But maybe somebody knows another proper way to make jQueryUI Datepicker understand PHP date format standard?
EDIT: I wrote a tutorial that explains both the problem and the solution. For further reading : http://tristan-jahier.fr/blog/2013/08/convertir-un-format-de-date-php-en-format-de-date-jqueryui-datepicker
I chose the brutal method : converting symbol-by-symbol the date format.
I made a 'not-so-dummy' code snippet.
/*
 * Matches each symbol of PHP date format standard
 * with jQuery equivalent codeword
* #author Tristan Jahier
 */
function dateformat_PHP_to_jQueryUI($php_format)
{
    $SYMBOLS_MATCHING = array(
        // Day
        'd' => 'dd',
        'D' => 'D',
        'j' => 'd',
        'l' => 'DD',
        'N' => '',
        'S' => '',
        'w' => '',
        'z' => 'o',
        // Week
        'W' => '',
        // Month
        'F' => 'MM',
        'm' => 'mm',
        'M' => 'M',
        'n' => 'm',
        't' => '',
        // Year
        'L' => '',
        'o' => '',
        'Y' => 'yy',
        'y' => 'y',
        // Time
        'a' => '',
        'A' => '',
        'B' => '',
        'g' => '',
        'G' => '',
        'h' => '',
        'H' => '',
        'i' => '',
        's' => '',
        'u' => ''
    );
    $jqueryui_format = "";
    $escaping = false;
    for($i = 0; $i < strlen($php_format); $i++)
    {
        $char = $php_format[$i];
        if($char === '\\') // PHP date format escaping character
        {
            $i++;
            if($escaping) $jqueryui_format .= $php_format[$i];
            else $jqueryui_format .= '\'' . $php_format[$i];
            $escaping = true;
        }
        else
        {
            if($escaping) { $jqueryui_format .= "'"; $escaping = false; }
            if(isset($SYMBOLS_MATCHING[$char]))
                $jqueryui_format .= $SYMBOLS_MATCHING[$char];
            else
                $jqueryui_format .= $char;
        }
    }
    return $jqueryui_format;
}
This function handles all the common codewords between PHP and Datepicker date format standards.
Plus, I added support for character escaping :
d m \o\f Y becomes dd mm 'of' yy
You may still have problems with symbols like 'W', 'L' that have no equivalent handled by Datepicker.
You cannot use the same format with datepicker that you're using with PHP.
Since PHP's date format only uses single letter codes, you're better off just taking the PHP date format and replacing each code to the corresponding value in the jQuery datepicker format, e.g.:
$PHPFormatOptions = array('y', 'Y', 'm', 'd');
$JSFormatOptions = array('yy', 'yyyy', 'mm', 'dd'); // and so on
$JSFormat = str_replace($PHPFormatOptions, $JSFormatOptions, $PHPFormat);
Not sure I'm quite with you, but this really shouldn't be an issue. You could either parse the front-end input: using DateTime::createFromFormat cf. php documentation for this, or use JSON.
Since JSON has an accepted standard way of formatting date strings, you can pass a JSON-stringified version of the input date to PHP, and json_decode it server-side. Both of these solutions are open to you, though I believe the first one to be easier to implement in your case.
If you want to be able to choose the format on both sides, the DateTime object is definitely what you need:
$date = new DateTime();
echo $date->format('Y-m-d').' <==> '.$date->format('y-M-j');
$postDate = $date->createFromFormat('y-m-d',$_POST['submitDate']);
echo $postDate->format('Y-m-d');
The format is explained on the page I've linked to.
Here is the solution:
function datepicker_format($format) {
static $assoc = array(
'Y' => 'yyyy',
'y' => 'yy',
'F' => 'MM',
'm' => 'mm',
'l' => 'DD',
'd' => 'dd',
'D' => 'D',
'j' => 'd',
'M' => 'M',
'n' => 'm',
'z' => 'o',
'N' => '',
'S' => '',
'w' => '',
'W' => '',
't' => '',
'L' => '',
'o' => '',
'a' => '',
'A' => '',
'B' => '',
'g' => '',
'G' => '',
'h' => '',
'H' => '',
'i' => '',
's' => '',
'u' => ''
);
$keys = array_keys($assoc);
$indeces = array_map(function($index) {
return '{{' . $index . '}}';
}, array_keys($keys));
$format = str_replace($keys, $indeces, $format);
return str_replace($indeces, $assoc, $format);
}
The magic double str_replace call caused by duplicating in needles and its replacement values, so that's why the string
m/d/Y
becomes
{{3}}/{{5}}/{{1}}
and after that this nests replacing with actual replacement values:
mm/dd/yy
Ok so the best solution for you would be to store everything in your website using time();
As far as I know datepicker can be set to work with dates for PHP timestamp
dateFormat : 'yy-mm-dd',
Edit :
Why would you store a date like : Y-m-d ?
It should be stored as timestamp or int
I ran into an issue with a data feed I need to import where for some reason the feed producer has decided to provide data that should clearly be either INT or FLOAT as strings-- like this:
$CASES_SOLD = "THREE";
$CASES_STOCKED = "FOUR";
Is there a way in PHP to interpret the text string as the actual integer?
EDIT: I should be more clear-- I need to have the $cases_sold etc. as an integer-- so I can then manipulate them as digits, store in database as INT, etc.
Use an associative array, for example:
$map = array("ONE" => 1, "TWO" => 2, "THREE" => 3, "FOUR" => 4);
$CASES_SOLD = $map["THREE"]; // 3
If you are only interested by "converting" one to nine, you may use the following code:
$convert = array('one' => 1,
'two' => 2,
'three' => 3,
'four' => 4,
'five' => 5,
'six' => 6,
'seven' => 7,
'eight' => 8,
'nine' => 9
);
echo $convert[strtolower($CASES_SOLD)]; // will display 3
If you only need the base 10 numerals, just make a map
$numberMap = array(
'ONE' => 1
, 'TWO' => 2
, 'THREE' => 3
// etc..
);
$number = $numberMap[$CASES_SOLD];
// $number == 3'
If you need something more complex, like interpreting Four Thousand Two Hundred Fifty Eight into 4258 then you'll need to roll up your sleeves and look at this related question.
Impress your fellow programmers by handling this in a totally obtuse way:
<?php
$text = 'four';
if(ereg("[[.$text.]]", "0123456789", $m)) {
$value = (int) $m[0];
echo $value;
}
?>
You need a list of numbers in english and then replace to string, but, you should play with 'thousand' and 'million' clause where must check if after string 'thousend-three' and remove integer from string.
You should play with this function and try change if-else and add some functionality for good conversion:
I'm writing now a simple code for basic, but you know others what should change, play!
Look at million, thousand and string AND, it should be change if no in string like '1345'. Than replace with str_replace each of them separaterly and join them to integer.
function conv($string)
{
$conv = array(
'ONE' => 1,
'TWO' => 2,
'THREE' => 3,
'FOUR' => 4,
'FIVE' => 5,
'SIX' => 6,
'SEVEN' => 7,
'EIGHT' => 8,
'NINE' => 9,
'TEN' => 10,
'ELEVEN' => 11,
'TWELVE' => 12,
'THIRTEEN' => 13,
'FOURTEEN' => 14,
'FIFTEEN' => 15,
'SIXTEEN' => 16,
'SEVENTEEN' => 17,
'EIGHTEEN' => 18,
'NINETEEN' => 19,
'TWENTY' => 20,
'THIRTY' => 30,
'FORTY' => 40,
'FIFTY' => 50,
'SIXTY' => 60,
'SEVENTY' => 70,
'EIGTHY' => 80,
'NINETY' => 90,
'HUNDRED' => 00,
'AND' => '',
'THOUSAND' => 000
'MILLION' => 000000,
);
if (stristr('-', $string))
{
$val = explode('-', $string);
#hardcode some programming logic for checkers if thousands, should if trim zero or not, check if another values
foreach ($conv as $conv_k => $conv_v)
{
$string[] = str_replace($conv_k, $conv_v, $string);
}
return join($string);
}
else
{
foreach ($conv as $conv_k => $conv_v)
{
$string[] = str_replace($conv_k, $conv_v, $string);
}
return join($string);
}
}
Basically what you want is to write a parser for the formal grammar that represents written numbers (up to some finite upper bound). Depending on how high you need to go, the parser could be as trivial as
$numbers = ('zero', 'one', 'two', 'three');
$input = 'TWO';
$result = array_search(strtolower($input), $numbers);
...or as involved as a full-blown parser generated by a tool as ANTLR. Since you probably only need to process relatively small numbers, the most practical solution might be to manually hand-code a small parser. You can take a look here for the ready-made grammar and implement it in PHP.
This is similar to Converting words to numbers in PHP
PHP doesn't have built in conversion functionality. You'd have to build your own logic based on switch statements or otherwise.
Or use an existing library like:
http://www.phpclasses.org/package/7082-PHP-Convert-a-string-of-English-words-to-numbers.html