How to convert array to date format in PHP? [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 3 years ago.
I am getting an array in PHP as:
Array
(
[1] => 2019
[2] => 5
[3] => 7
[4] => 0
)
where [1] is always the year, [2] is always the month and [3] is always the date.
How can I convert this array to date("Y-m-d") format?

Assuming this data input:
$data = [null, 2019, 5, 7, 0];
Using DateTime
$dt = new DateTime(sprintf( "%04d-%02d-%02d", $data[1], $data[2],
$data[3]));
echo $dt->format('Y-m-d') . "\n";
Using Sprintf
// use this if you really trust the data
$dt = sprintf( "%04d-%02d-%02d", $data[0], $data[1], $data[2]);
echo $dt . "\n";
Using Carbon
// Carbon is a fantastic Date and Time class -> https://carbon.nesbot.com/
$dt = \Carbon\Carbon::create($data[0], $data[1], $data[2], 0, 0, 0);
echo $dt->format('Y-m-d') . "\n";

you can use DateTime
$timeArray = [2019,5,7,0];
$dateTime = new DateTime(printf( "%d-%d-%d", $timeArray[0],$timeArray[1],$timeArray[2] ));
echo $dateTime->format('Y-m-d'); // output: 2019-05-07

Do it like this
$arr = array( '2019', '5', '7', '0' );
echo date('Y-m-d',strtotime("$arr[0]/$arr[1]/$arr[2]"));

Although it's possible to just concatenate those values into a string and then let PHP parse that string into the Y-m-d format, I personally think mktime() is the better solution:
echo date("Y-m-d", mktime(0, 0, 0, $arr[2], $arr[3], $arr[1]));
// 2019-05-07
This removes the risk of PHP accidentally interpreting the day and month in the wrong order.

You might simply use concat and join them into a string:
$arr = array(
"1" => "2019",
"2" => "5",
"3" => "7",
"4" => "0",
);
$datetime_format = $arr["1"] . "-" . $arr["2"] . "-" . $arr["3"];
var_dump($datetime_format);
Output
string(8) "2019-5-7"
If you wish to have a 4-2-2 format, this might work:
$arr = array(
"1" => "2019",
"2" => "5",
"3" => "7",
"4" => "0",
);
$datetime_format = '';
foreach ($arr as $key => $value) {
if ($key == "4") {break;}
echo strlen($value);
if (strlen($value) >= 2) {
$datetime_format .= $value;
} elseif (strlen($value) == 2) {
$datetime_format .= $value;
} elseif (strlen($value) == 1) {
$datetime_format .= "0" . $value;
} else {
echo "Something is not right!";
}
if ($key <= "2") {$datetime_format .= '-';}
}
var_dump($datetime_format);
Output
string(10) "2019-05-07"

Related

Get Date Range Of financial Year in php

How can I get the Financial Year date range in PHP like below when I pass year and return date range of every year start and end.
Like Eg.
Input Array = [2017,2018]
Financial Start Month = 04
Output Array =
[
'2017' => [
'start' => '2016-04-01',
'end' => '2017-03-31'
],
'2018' => [
'start' => '2017-04-01',
'end' => '2018-03-31'
]
]
My Effort:-
$year_arr = [2017,2018];
$fn_month = 04;
$date_range_arr = [];
foreach ($year_arr as $key => $value) {
$fn_start_date_year = ($value - 1);
$fn_start_date_month = $fn_month;
$fn_start_date_day = '01';
$fn_start_date_string = $fn_start_date_year.'-'.$fn_start_date_month.'-'.$fn_start_date_day;
$start_date = date('Y-m-d',strtotime($fn_start_date_string));
$fn_end_date_year = ($value);
$fn_end_date_month = (fn_month == 1)?12:(fn_month-1);
$fn_end_date_day = date('t',strtotime($fn_end_date_year.'-'.$fn_end_date_month.'-01'));
$fn_start_date_string = $fn_end_date_year.'-'.$fn_end_date_month.'-'.$fn_end_date_day;
$end_date = date('Y-m-d',strtotime($fn_start_date_string));
$date_range_arr[$value] = [
'start_date' => $start_date,
'end_date' => $end_date
];
}
Above is my effort. It is working perfectly but needs a more robust code.
A good way to manipulate dates in PHP is using the DateTime class. Here's an example of how to get the results you want using it. By using the modify method, we can avoid worries about complications like leap years (see the result for 2016 below).
$year_arr = [2016,2017,2018];
$fn_month = 03;
foreach ($year_arr as $year) {
$end_date = new DateTime($year . '-' . $fn_month . '-01');
$start_date = clone $end_date;
$start_date->modify('-1 year');
$end_date->modify('-1 day');
$date_range_arr[$year] = array('start_date' => $start_date->format('Y-m-d'),
'end_date' => $end_date->format('Y-m-d'));
}
print_r($date_range_arr);
Output:
Array (
[2016] => Array (
[start_date] => 2015-03-01
[end_date] => 2016-02-29
)
[2017] => Array (
[start_date] => 2016-03-01
[end_date] => 2017-02-28
)
[2018] => Array (
[start_date] => 2017-03-01
[end_date] => 2018-02-28
)
)
Demo on 3v4l.org
Maybe this is what you need?
I use strtotime to parse the date strings.
$year_arr = [2017,2018];
$fn_month = 04;
$date_range_arr = [];
foreach($year_arr as $year){
$date_range_arr[$year] =['start' => date("Y-m-d", strtotime($year-1 . "-" .$fn_month . "-01")),
'end' => date("Y-m-d", strtotime($year . "-" .$fn_month . "-01 - 1 day"))];
}
var_dump($date_range_arr);
Output:
array(2) {
[2017]=>
array(2) {
["start"]=>
string(10) "2016-04-01"
["end"]=>
string(10) "2017-03-31"
}
[2018]=>
array(2) {
["start"]=>
string(10) "2017-04-01"
["end"]=>
string(10) "2018-03-31"
}
}
https://3v4l.org/nMUHt
Try this snippet,
function pr($a)
{
echo "<pre>";
print_r($a);
echo "</pre>";
}
$year_arr = [2017, 2018];
$fn_month = 4;
$date_range_arr = [];
foreach ($year_arr as $key => $value) {
$fn_month = str_pad(intval($fn_month),2, 0, STR_PAD_LEFT);
$date = "".($value-1)."-$fn_month-01"; // first day of month
$date_range_arr[$value] = [
'start_date' => $date,
'end_date' => date("Y-m-t", strtotime($date.' 11 months')), // last month minus and last date of month
];
}
pr($date_range_arr);
die;
str_pad - Pad a string to a certain length with another string
Here is working demo.

Grouping with hyphenate not working with i18N for weekdays

I am working on a snippet for displaying opening hours and it works fine in english language and when I change the keys of array to another language it doesn't hyphenate the letters instead it does separation by comma.
What am I doing Wrong?
Below is the PHP code with 2 arrays with 1 commented which is in english and which works fine. Another is an italian langugage weekdays
<?php
/*
// english weekdays
$openHours = array(
'Mon' => '9am-7pm',
'Tue' => '9am-7pm',
'Wed' => '9am-7pm',
'Thu' => '9am-10pm',
'Fri' => 'closed',
'Sat' => '9am-10pm',
'Sun' => '9am-10pm'
);
*/
// italian weekdays
$openHours = array(
'lunedì' => '9am-7pm',
'martedì' => '9am-7pm',
'mercoledì' => '9am-7pm',
'giovedì' => '9am-10pm',
'venerdì' => 'closed',
'sabato' => '9am-10pm',
'domenica' => '9am-10pm'
);
$new_array = array();
foreach($openHours as $key => $value)
{
if(in_array($value,$new_array))
{
$key_new = array_search($value, $new_array);//to get the key of element
unset($new_array[$key_new]); //remove the element
$key_new = $key_new.','.$key; //updating the key
$new_array[$key_new] = $value; //inserting new element to the key
}
else
{
$new_array[$key] = $value;
}
}
foreach ($new_array as $days=>$time){
$daylist = explode(',',$days);
if ($time!='closed'){
if (count($daylist)>2){
$limit = count($daylist)-1;
$first = $daylist[0];
$last = $daylist[$limit];
//loop will go here.
if (date('D', strtotime('+'.$limit.' days', strtotime($first)))==$last){
echo $first.'-'.$last.' '.$time.'<br>';
} else {
$sep = '';
foreach ($daylist as $sepdays){
echo $sep.$sepdays;
$sep = ',';
}
echo ' '.$time.'<br>';
}
} else {
echo $days.' '.$time.'<br>';
}
} else {
$daylist = explode(',',$days);
foreach ($daylist as $sepdays){
echo $sepdays.' '.$time.'<br>';
}
}
}
?>
RESULT
Current Result what am getting with italian language.
lunedì,martedì,mercoledì 9am-7pm
venerdì closed
giovedì,sabato,domenica 9am-10pm
Expected RESULT
This is what I'm expecting.
lunedì-mercoledì 9am-7pm
venerdì closed
giovedì,sabato,domenica 9am-10pm
You are using your array's keys within date and strtotime functions to do your comparisons, both functions works for English. If you need to do it on other languages you should use setlocale and strftime, it will be a lot more complicated process. My suggestions:
Use numeric representation of the days of the week (0-6) and on display, replace the number with the value for the desired language.
Use multidimensional arrays including the numeric day of the week and the opening hours.

Sort array in chronological order with date as key -php

I basically have this code which determines the number of same dates in an array:
function get_archives_html ($blog) {
//tag array to store arrays with same tag
$count_tags = array();
//store output
$output = '';
foreach($blog as $id) {
//abbreviate date name
$originalDate = $id["date"]["year"].'-'.$id["date"]["month"].'-'.$id["date"]["day"];
$abbrevDate = date("F, Y", strtotime($originalDate));
if(!isset($count_tags[$abbrevDate])) {
$count_tags[$abbrevDate] = 0;
}
++$count_tags[$abbrevDate];
}
// Sort your tags from hi to low
//arsort($count_tags);
var_dump($count_tags);
foreach($count_tags as $month=>$id) {
$output.= '<p>'. $month.($id > 1 ? ' ('.$id .')' : '').'</p>';
}
return $output;
}
The output would look like this:
$arr = array(
"November, 2016" => "2",
"October, 2016" => "5",
"October, 2017" => "3",
"September, 2017" => "6"
);
Now, I use the keys to display it on the html. Problem is that it is not arranged properly by date but rather by alphabet.
So, my question is, how can I sort this array by key and by date. Example would be October, 2016, November 2016, September, 2017, October 2014 Thank you
<?php
$arr = array(
"November, 2016" => "2",
"October, 2016" => "5",
"October, 2017" => "3",
"September, 2017" => "6"
);
//Sort by ascending date in the key
uksort($arr,function($a,$b){
return strtotime(strtr($a,',',' '))<=>strtotime(strtr($b,',',' '));
});
//result
$expected = array (
'October, 2016' => "5",
'November, 2016' => "2",
'September, 2017' => "6",
'October, 2017' => "3",
);
var_dump($arr === $expected); //bool(true)

PHP find string value and marked it by position in array

I have an array like this
array:32 [▼
"ID" => "7917"
"ProvinceCode" => "MB"
"Create" => "2016-05-18 18:16:26.790"
"DayOfTheWeek" => "4"
"Giai1" => "28192"
"Giai2" => "83509"
"Giai3" => "51911-02858"
"Giai4" => "14102-97270-96025-08465-89047-45904"
"Giai5" => "7892-9140-4069-8499"
"Giai6" => "6117-7471-5541-9119-4855-0566"
"Giai7" => "843-860-023"
"Giai8" => "71-13-55-89"
"Giai9" => ""
"Status" => "1"
]
I have a int variable $position = 59, and my job is find value by counting characters from Giai1 to Giai9 for 59 times count from 0 and get value of this position not include character -, so if $position = 59 then the getted value at position 58 will return.
For example, find value at position 20, the return is 1 at 14102 in Giai4 (actually 19 count from 0)
I've been wrote this code to do this
$position = 59;
$count = 0;
foreach ($data['result'][0] as $key => $item)
{
if(preg_match('#Giai#s', $key))
{
$_item = str_replace('-', '', $item);
$count = $count + strlen($_item);
$chars = str_split($item);
$chars_sp = array_count_values($chars);
$countChar = count($chars);
if($count > $position)
{
//this block contains needed position
$math = $count - $position;
$secmath = strlen($_item) - $math;
for($i=$secmath;$i>=0;$i--){
if($chars[$i] == '-'){
$splash_last++;
}
}
$secmath = $secmath + $splash_last;
if($chars[$secmath] == '-'){
echo "+1 - ";
$secmath = $secmath + 1;
}
echo "Count: $count Match: $math Secmatch: $secmath Splash_last: $splash_last";
$chars[$secmath] = 'x' . $chars[$secmath] . 'y';
$edited = implode('', $chars);
$data['result'][0][$key] = $edited;
break;
}
}
}
dd($data['result'][0]);
}
Expected result will return this array with the mark of getted number.
For example, code found number at position 59 (58 from 0) and signed it by x at first and y at end of value. You can see this in Giai5
//This is expected result
//Result array with mark of value at needed position
array:32 [▼
"ID" => "7917"
"ProvinceCode" => "MB"
"Create" => "2016-05-18 18:16:26.790"
"DayOfTheWeek" => "4"
"Giai1" => "28192"
"Giai2" => "83509"
"Giai3" => "51911-02858"
"Giai4" => "14102-97270-96025-08465-89047-45904"
"Giai5" => "7892-9140-x4y069-8499"
"Giai6" => "6117-7471-5541-9119-4855-0566"
"Giai7" => "843-860-023"
"Giai8" => "71-13-55-89"
"Giai9" => ""
"Status" => "1"
]
from 1 to 50 it works fine, but after position 50, the value of position I get is always wrong
Any idea?
If I understood you correctly this time, you can do something like this:
$array = [
"ID" => "7917",
"ProvinceCode" => "MB",
"Create" => "2016-05-18 18:16:26.790",
"DayOfTheWeek" => "4",
"Giai1" => "28192",
"Giai2" => "83509",
"Giai3" => "51911-02858",
"Giai4" => "14102-97270-96025-08465-89047-45904",
"Giai5" => "7892-9140-4069-8499",
"Giai6" => "6117-7471-5541-9119-4855-0566",
"Giai7" => "843-860-023",
"Giai8" => "71-13-55-89",
"Giai9" => "",
"Status" => "1"
];
$position = 59;
$start = 0;
$end = 0;
foreach ($array as $key => &$value) {
if (!preg_match('/Giai/', $key)) {
continue;
}
$start = $end + 1;
$end = $start + strlen(str_replace('-', '', $value)) - 1;
if (($start <= $position) && ($position <= $end)) {
$counter = $start;
$value = str_split($value);
foreach ($value as &$char) {
if ($char === '-') {
continue;
}
if ($counter === $position) {
$char = "x{$char}y";
break;
}
$counter++;
}
$value = join($value);
}
}
var_dump($array);
Here is demo.
The code is a bit lengthy, but this is due to the fact that when you watch for the position you skip the dashes (-), but when you need to mark the character you have to take them into account. From this code you can understand the algorithm and then refactor code the way you want. I would suggest to escape from nested loops, as they are hard to read. You can do this by breaking code into functions or use the available array functions.
You can achieve this with a simple array_grep() to fetch all the "Giai" keys and implode to concatenate the values to one big string, then selecting a position of that string.
$giai = array_flip(preg_grep("/^Giai\d+$/", array_flip($a)));
echo implode("",$giai)[$pos];
https://eval.in/573746

Display dates in Arabic

Here's my code:
setlocale( LC_ALL,'ar' );
echo strftime( '%e %b, %Y', strtotime( '2011-10-25' ));
Output:
25 Sep, 2011
Why is it not displaying the arabic date? Am I using strftime incorrectly?
Here you can print the Arabic PHP Date :
Create a file called arabicdate.php and place this function inside it :
function ArabicDate() {
$months = array("Jan" => "يناير", "Feb" => "فبراير", "Mar" => "مارس", "Apr" => "أبريل", "May" => "مايو", "Jun" => "يونيو", "Jul" => "يوليو", "Aug" => "أغسطس", "Sep" => "سبتمبر", "Oct" => "أكتوبر", "Nov" => "نوفمبر", "Dec" => "ديسمبر");
$your_date = date('y-m-d'); // The Current Date
$en_month = date("M", strtotime($your_date));
foreach ($months as $en => $ar) {
if ($en == $en_month) { $ar_month = $ar; }
}
$find = array ("Sat", "Sun", "Mon", "Tue", "Wed" , "Thu", "Fri");
$replace = array ("السبت", "الأحد", "الإثنين", "الثلاثاء", "الأربعاء", "الخميس", "الجمعة");
$ar_day_format = date('D'); // The Current Day
$ar_day = str_replace($find, $replace, $ar_day_format);
header('Content-Type: text/html; charset=utf-8');
$standard = array("0","1","2","3","4","5","6","7","8","9");
$eastern_arabic_symbols = array("٠","١","٢","٣","٤","٥","٦","٧","٨","٩");
$current_date = $ar_day.' '.date('d').' / '.$ar_month.' / '.date('Y');
$arabic_date = str_replace($standard , $eastern_arabic_symbols , $current_date);
return $arabic_date;
}
Now include this file in your page :
include 'arabicdate.php';
Then you can print the Arabic PHP Date :
echo ArabicDate();
Live Formatted Example :
http://ideone.com/MC0hou
Hope that helps.
How about this:
function arabicDate($time)
{
$months = ["Jan" => "يناير", "Feb" => "فبراير", "Mar" => "مارس", "Apr" => "أبريل", "May" => "مايو", "Jun" => "يونيو", "Jul" => "يوليو", "Aug" => "أغسطس", "Sep" => "سبتمبر", "Oct" => "أكتوبر", "Nov" => "نوفمبر", "Dec" => "ديسمبر"];
$days = ["Sat" => "السبت", "Sun" => "الأحد", "Mon" => "الإثنين", "Tue" => "الثلاثاء", "Wed" => "الأربعاء", "Thu" => "الخميس", "Fri" => "الجمعة"];
$am_pm = ['AM' => 'صباحاً', 'PM' => 'مساءً'];
$day = $days[date('D', $time)];
$month = $months[date('M', $time)];
$am_pm = $am_pm[date('A', $time)];
$date = $day . ' ' . date('d', $time) . ' - ' . $month . ' - ' . date('Y', $time) . ' ' . date('h:i', $time) . ' ' . $am_pm;
$numbers_ar = ["٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"];
$numbers_en = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
return str_replace($numbers_en, $numbers_ar, $date);
}
Note: the parameter ($time) should be Unix timestamp.
AFAIK setlocale won't actually do any language translation for you but rather affects things like the formatting and comparator functionality. If you want localisation then you could try using IntlDateFormatter which may give you what you need.
Updated: You could also try Zend_Date as suggested in this question if PHP 5.3 isn't an option for you.
Inspired by Amr SubZero's answer above:
If anybody else needed this, these two functions displays post date and time in arabic for a wordpress website:
DATE:
functions.php
function single_post_arabic_date($postdate_d,$postdate_d2,$postdate_m,$postdate_y) {
$months = array("Jan" => "يناير", "Feb" => "فبراير", "Mar" => "مارس", "Apr" => "أبريل", "May" => "مايو", "Jun" => "يونيو", "Jul" => "يوليو", "Aug" => "أغسطس", "Sep" => "سبتمبر", "Oct" => "أكتوبر", "Nov" => "نوفمبر", "Dec" => "ديسمبر");
$en_month = $postdate_m;
foreach ($months as $en => $ar) {
if ($en == $en_month) { $ar_month = $ar; }
}
$find = array ("Sat", "Sun", "Mon", "Tue", "Wed" , "Thu", "Fri");
$replace = array ("السبت", "الأحد", "الإثنين", "الثلاثاء", "الأربعاء", "الخميس", "الجمعة");
$ar_day_format = $postdate_d2;
$ar_day = str_replace($find, $replace, $ar_day_format);
header('Content-Type: text/html; charset=utf-8');
$standard = array("0","1","2","3","4","5","6","7","8","9");
$eastern_arabic_symbols = array("٠","١","٢","٣","٤","٥","٦","٧","٨","٩");
$post_date = $ar_day.' '.$postdate_d.' '.$ar_month.' '.$postdate_y;
$arabic_date = str_replace($standard , $eastern_arabic_symbols , $post_date);
return $arabic_date;
}
Inside the loop:
<date>
<?php
$postdate_d = get_the_date('d');
$postdate_d2 = get_the_date('D');
$postdate_m = get_the_date('M');
$postdate_y = get_the_date('Y');
echo single_post_arabic_date($postdate_d,$postdate_d2, $postdate_m, $postdate_y);
?>
</date>
TIME:
functions.php
function single_post_arabic_time($posttime_h, $posttime_i, $posttime_a) {
$ampm = array("AM", "PM");
$ampmreplace = array("ق.ظ", "ب.ظ");
$ar_ampm = str_replace($ampm, $ampmreplace, $posttime_a);
header('Content-Type: text/html; charset=utf-8');
$standardletters = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
$eastern_arabic_letters = array("٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩");
$post_time = $posttime_h . ':' . $posttime_i." ".$ar_ampm;
$arabic_time = str_replace($standardletters, $eastern_arabic_letters, $post_time);
return $arabic_time;
}
Inside the loop:
<span>الساعة </span>
<time>
<?php
$posttime_h = get_the_date('h');
$posttime_i = get_the_date('i');
$posttime_s = get_the_date('d');
$posttime_a = get_the_date('A');
echo single_post_arabic_time($posttime_h,$posttime_i,$posttime_a);
?>
</time>
if all you're looking for is to print what day is today, then your question is easy...
Try this function.
<?php
function arDate(){
$MONTHS = array('كانون الثاني','شباط','آذار','نيسان','أيار','حزيران','تموز','آب','أيلول','تشرين الأول','تشرين الثاني','كانون الأول');
$DAYS = array('الأحد','الاثنين','الثلاثاء','الأربعاء','الخميس','الجمعة','السبت');
$dName = date("w"); // the number of the week-day ((from 0 to 6)). [0] for Sunday, [6] for Saturday //
$dm = date("d"); // day of the month in numbers without leading zero; i.e.: 1, 2, 3... 28, 29, 30 //
$mnth = date("n")-1; // number of the month ((from 1 to 12)) this is why we minus 1 from it so that it align with our $MONTHS array.;
$yr = date('Y'); // four-digit year; eg.: 1981 //
return $DAYS[$dName] . " " . $dm . " / " . $MONTHS[$mnth] . " / " . $yr;
}
$today = arDate();
echo $today; // الأحد 01 / آب / 2021
?>
EXPLANATION:
We first prepare two arrays with arabic names of both the days and months. Then we get four variables using the PHP built-in function date(). This function has lots of parameters to control its return. I'm here using the parameters that would give me numbers so that I use them as indexes in the $MONTHS[bla bla bla] and $DAYS[bla bla bla] vars. Finally, format your arabic date to your heart content!
have a look at PHP date() function in here
NOTE1:
Do notice, please, that you can play with the arrangement of the days and months so that you don't need to minus one from your variables (-1) as I did above. Refer to the link of W3S and you would understand how to organize your arabic-name ARRAYS.
NOTE2:
Also, notice please that I'm using the Classical Arabic names in my function and which are used in Syria only; they are not so well-known in the rest of the Arab-league states though they are the classical names for months in Arabic.
Have you run
locale -a
and verified that your system has a locale called "ar"? It might be called something more specific, e.g. "ar_AR.utf8"... If you need to support Arabic locale spelled differently in multiple systems, you may pass an array to setlocale(). The first locale name in that array that the system supports will be used.
I use this javascript function if i can help:
<script type='text/javascript'>
navig = navigator.appName;
versn = parseInt(navigator.appVersion);
if ( (navig == "Netscape" && versn >= 3) || (navig == "Microsoft Internet Explorer" && versn >= 4))
info = "true";
else info = "false";
function Ar_Date() {
if (info == "true") {
var info3 = new Date();
var info4=info3.getDay();
var info5=info3.getMonth();
var info6=info3.getDate();
var info7=info3.getFullYear();
var info8 = new Array('لأحد','الإثنين','الثلاثاء','الأربعاء','الخميس','الجمعة','السبت');
var info9 = info8[info4];
var info10 = new Array('جانفي','فيفري','مارس','أفريل','ماي','جوان','جويلية','أوت','سبتمبر','أكتوبر','نوفمبر','ديسمبر');
var info11 = info10[info5];
var info12=info9+'، '+info6+' '+info11+' '+info7;
var info12=info9+'، '+info6+' '+info11;
document.write(info12);
}
}
</script>
function single_post_arabic_date($postdate_d,$postdate_d2,$postdate_m,$postdate_y) {
$months = array("01" => "يناير", "02" => "فبراير", "03" => "مارس", "04" => "أبريل", "05" => "مايو", "06" => "يونيو", "07" => "يوليو", "08" => "أغسطس", "09" => "سبتمبر", "10" => "أكتوبر", "11" => "نوفمبر", "12" => "ديسمبر");
$ar_month =months[$postdate_m];
$find = array ("Sat", "Sun", "Mon", "Tue", "Wed" , "Thu", "Fri");
$replace = array ("السبت", "الأحد", "الإثنين", "الثلاثاء", "الأربعاء", "الخميس", "الجمعة");
$ar_day_format = $postdate_d2;
$ar_day = str_replace($find, $replace, $ar_day_format);
header('Content-Type: text/html; charset=utf-8');
$standard = array("0","1","2","3","4","5","6","7","8","9");
$eastern_arabic_symbols = array("٠","١","٢","٣","٤","٥","٦","٧","٨","٩");
$post_date = $ar_day.' '.$postdate_d.' '.$ar_month.' '.$postdate_y;
$arabic_date = str_replace($standard , $eastern_arabic_symbols , $post_date);
return $arabic_date;
}
this is just improve function
<?php
$postdate_d = get_the_date('d');
$postdate_d2 = get_the_date('D');
$postdate_m = get_the_date('m');
$postdate_y = get_the_date('Y');
echo single_post_arabic_date($postdate_d,$postdate_d2, $postdate_m, $postdate_y);
?>
This should work:
setLocale(LC_ALL , 'ar_EG.utf-8');
If dates are still not displayed in Arabic, Then the arabic locale may not be installed on the system, To check it,connect using a terminal and type: locale -a, it would display the installed locales, if Arabic is not listed, you have to install it first and then it should work.
/**
* Convert time string to arabic
*#param string $time
*/
public function arabicDate($time)
{
$en_data = ['January', 'Jan', 'Feburary', 'Feb', 'March', 'Mar',
'April', 'Apr', 'May', 'June', 'Jun',
'July', 'Jul', 'August', 'Aug', 'September', 'Sep',
'October', 'Oct', 'November', 'Nov', 'December', 'Dec',
'Satureday', 'Sat', 'Sunday', 'Sun', 'Monday', 'Mon',
'Tuesday', 'Tue', 'Wednesday', 'Wed', 'Thursday', 'Thu', 'Friday', 'Fri',
'AM', 'am', 'PM', 'pm'
];
$ar_data = ['يناير', 'يناير', 'فبراير', 'فبراير', 'مارس', 'مارس',
'أبريل', 'أبريل', 'مايو', 'مايو', 'يونيو', 'يونيو',
'يوليو', 'يوليو', 'أغسطس', 'أغسطس', 'سبتمبر', 'سبتمبر',
'أكتوبر', 'أكتوبر', 'نوفمبر', 'نوفمبر', 'ديسمبر', 'ديسمبر',
'السبت', 'السبت', 'الأحد', 'الأحد', 'الإثنين', 'الإثنين',
'الثلاثاء', 'الثلاثاء', 'الأربعاء', 'الأربعاء', 'الخميس', 'الخميس', 'الجمعة', 'الجمعة',
'صباحاً', 'صباحاً', 'مساءً', 'مساءً'
];
return str_replace($en_data, $ar_data, $time);
}
<?php
$date = '21 Dec 22 14:13';
$date_time = new DateTime($date);
$formatter = new IntlDateFormatter('ar_DZ',);
print $formatter->format($date_time);
For more reference refer this link.
Does this work for you:
setlocale(LC_ALL,'ar');
echo strftime('%A %d %B %Y');
Hope it helps

Categories