strtotime Translations - php

Working on a Wordpress theme that I had to convert the locale to french.
$data_event = get_post_meta($post->ID, 'event_date_interval', true);
$time = strtotime($data_event);
$pretty_date_yy = date('Y', $time);
setlocale (LC_ALL, "fr_FR");
$translate_fr = strftime("%h", strtotime($data_event));
$pretty_date_M = htmlentities($translate_fr);
$pretty_date_d = date('d', $time);
This works fine, it shows everything as it should (For example, right now we are in February so it shows FÉVR)
However my problem lies in that I want it to show FÉV. and not FÉVR. Is it possible to change this abbreviation?
EDIT: Solution was to create an array and set the specific names I wanted. It was not encoding properly utf8_encode was added. Thanks Phex!
$data_event = get_post_meta($post->ID, 'event_date_interval', true);
$time = strtotime($data_event);
setlocale (LC_ALL, "fr_FR");
$pretty_date_yy = date('Y', $time);
$pretty_date_d = date('d', $time);
$id = intval(strftime("%m", strtotime($data_event))) - 1;
$abr_map = array(
'Jan',
'Fév',
'Mar',
'Avr',
'Mai',
'Juin',
'Juil',
'Aout',
'Sept',
'Oct',
'Nov',
'Déc'
);
$translate_fr = htmlentities(utf8_decode($abr_map[$id]));
$pretty_date_M = $translate_fr;

One way to do it would be to use PHPs substr function as follows:
$translate_fr = substr(strftime("%h", strtotime($data_event)), 0, 3);
Edit:
In case not all months should be abbreviated to three characters, it would be possible to use an associative array as a map:
$abr_map = array(
'JANV' => 'Jan',
'FÉVR' => 'Fév',
'MARS' => 'Mar',
'AVRI' => 'Avr',
'MAI' => 'Mai',
'JUIN' => 'Juin',
'JUIL' => 'Juil',
'AOUT' => 'Aout',
'SEPT' => 'Sept',
'OCTO' => 'Oct',
'NOVE' => 'Nov',
'DÉCE' => 'Déc'
);
Alternatively using intval and strftimes %m formatter to provide an integer "key" for an indexed array:
$id = intval(strftime("%m", strtotime($data_event))) - 1;
$abr_map = array(
'Jan',
'Fév',
'Mar',
'Avr',
'Mai',
'Juin',
'Juil',
'Aout',
'Sept',
'Oct',
'Nov',
'Déc'
);
To use the map within the function, you would then use
$translate_fr = htmlentities(utf8_decode($abr_map[$id]));
or alternatively using htmlentities built in encoder:
$translate_fr = htmlentities($abr_map[$id], ENT_COMPAT, 'UTF-8');

Related

Carbon - Laravel - Separate by Days

Look guys , i have two functions.
public function date(){
BusinessTime::enable(Carbon::class);
BusinessTime::enable(Carbon::class, [
'sunday' => ['08:00-22:00'],
'monday' => ['08:00-22:00'],
'tuesday' => ['08:00-22:00'],
'wednesday' => ['08:00-22:00'],
'thursday' => ['08:00-22:00'],
'friday' => ['08:00-23:59'],
'saturday' => ['00:00-23:59'],
'holidaysAreClosed' => true,
]);
$options = 0;
$date1 = Carbon::parse('2020-11-20 20:00')->diffInBusinessHours('2020-11-22 16:00:00', $options);
$date2 = $this->date2();
$date3 = $date2 - $date1;
echo 'Hours In'.round($date1);
echo '<br>';
echo 'Horas Out'.round($date3);
}
that return me all the hours , that are inside my range of bussiness hours .
and i have another function that return me all time between two dates .
public function date2(){
$start = Carbon::create(2020,11,20,20,0,0,'America/Recife');
$end = Carbon::create(2020,11,22,16,0,0,'America/Recife');
$options = 0;
return $start->diffInHours($end);
}
but how could i return on fuction date() , for example returne the diffInBusinessHours separate by day , where could be a array of date and hours . its possible ?
Are you using kylekatarnls/business-time?
If so, have you tried using $carbonDate->getOpeningHours() or Carbon::getOpeningHours()?

fpdf multicell not showing values

I am creating a pdf which is working but the values are not being entered into the multicell of the pdf.
Lets take the date for example, the user enters a date and on the PDF it should look like:
Date: 26/01/2017
but this is what it looks like now:
Date:
Below is my code with the date example:
<?php
//set the question values
$questions = array(
'name' => "Name: ",
'date' => "Date: ",
'first' => "First Day of Leave: ",
'last' => "Last Day of Leave: ",
'days' => "Number of Days Taken: ",
'email' => "Managers Email: ",
'creditdays' => "Leave Days in Credit",
'personnel' => "THIS SECTION TO BE COMPLETED BY PERSONNEL DEPARTMENT",
'credit' => "Leave Days in Credit:",
'mansign' => "Signed:",
'date2' => "Date:",
'authorise' => "Authorised By:",
'date3' => "Date:",
'auth' => "Authorisation Of Leave"
);
//set the question answers
$date = $_POST['date'];
$first = $_POST['first'];
$last = $_POST['last'];
$days = $_POST['days'];
$email = $_POST['email'];
$name = $_POST['name'];
//set the question names
$questionName = $questions['name'];
$questionDate = $questions['date'];
$questionFirst = $questions['first'];
$questionLast = $questions['last'];
$questionDays = $questions['days'];
$questionEmail = $questions['email'];
$personnel = $questions['personnel'];
$credit = $questions['credit'];
$mansign = $questions['mansign'];
$date2 = $questions['date2'];
$authorise = $questions['authorise'];
$date3 = $questions['date3'];
$auth = $questions['auth'];
//Create the PDF
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 16);
//insert fields
$pdf->SetDrawColor(100, 100, 100);
$pdf->SetFillColor(100,100,100);
$pdf->Multicell(200, 3, "Leave Application Form", "", 'C');
$pdf->Ln();
$pdf->Ln();
$pdf->MultiCell(190, 10, $questionDate, $date, 'C');
According to the fpdf manual:
MultiCell(float w, float h, string txt [, mixed border [, string align [, boolean fill]]])
While in your code:
$pdf->MultiCell(190, 10, $questionDate, $date, 'C');
So $date isn't part of the txt parameter to appear in the cell.
Instead, you need to append the $date variable to the $questionDate one.
$pdf->MultiCell(190, 10, $questionDate . $date, 'C');

PHP read file name, and the number in file name, echo heading with corresponding month

I have a php script that works great, but I want to echo a heading that lists the month it is for. The file names will have either a _01, _02, _03 etc. in them. I've created an array of $months, but I'm trying to figure out what the best way is to do this.
If file name contains _01, echo January. Else if file name contains _02, echo February. Anyone know of the best practice for this scenario?
foreach (glob("*.mov") as $filename)
$theData = file_get_contents($filename) or die("Unable to retrieve file data");
$months = ['_01', '_02', '_03', '_04', '_05', '_06', '_07', '_08', '_09', '_10', '_11', '_12'];
Try this:
foreach (glob("*.mov") as $filename)
$theData = file_get_contents($filename) or die("Unable to retrieve file data");
$months = ['January' => '_01', 'February' => '_02', 'March' => '_03', 'April' => '_04', 'May' => '_05', 'June' => '_06', 'July' => '_07', 'August' => '_08', 'September' => '_09', 'October' => '_10', 'November' => '_11', 'December' => '_12'];
foreach($months as $key => $month){
if(strpos($filename,$month)!==false){
echo $key;
}
}
maybe something similar to this - based on fact that month number will be in filename only once
foreach($theData as $filename){
preg_match('/_(\d{2})/', $filename, $match);
echo date('F',strtotime($match[1].'/20/2000'));
}
Here is what I was able to come up with
$files = [
"file_01",
"file_02asdf",
"file_03_sfsa",
"file_04_23",
"file_05 cat"
];
foreach($files as $file){
preg_match("/_(\d{2,2})/", $file, $matches);
echo date("F", strtotime("2000-{$matches[1]}-01"))."\n";
}
which results in:
January
February
March
April
May
maybe thats another way, but preg_match seems to be smarter
function getMonthnameByFilename($filename,$months) {
foreach($months as $index => $m) {
if(strpos($filename,$m) !== false) {
return date("F",strtotime("2013-".($index+1)."-1"));
}
}
return false;
}

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

Logo change with date script

I just wondered if anybody can point me in the right direction: I'm looking to make a script whereby the logo on my site changes depending on the date; so for instance a haloween style one soon.
I started off by having 2 arrays, 1 of start dates and 1 of end dates(not sure even if this is the best way!):
<?php
$start_dates = array('01/01' => 'New Years',
'14/02' => 'Valentine Day',
'16/02/2010' => 'Pancake Day',
'17/03' => 'St Patricks Day',
'01/04' => 'April Fools',
'02/04/2010' => 'Easter',
'23/04' => 'St Georges Day',
'11/06/2010' => 'World Cup',
'31/10' => 'Halloween',
'05/11' => 'Guy Fawkes',
'11/11' => 'Armistice Day',
'16/10' => 'Today',
'15/12' => 'Christmas');
$end_dates = array( '08/01' => 'New Years',
'15/02' => 'Valentine Day',
'17/02/2010' => 'Pancake Day',
'18/03' => 'St Patricks Day',
'02/04' => 'April Fools',
'06/04/2010' => 'Easter',
'24/04' => 'St Georges Day',
'12/07/2010' => 'World Cup',
'01/11' => 'Halloween',
'06/11' => 'Guy Fawkes',
'12/11' => 'Armistice Day',
'17/10' => 'Today',
'01/01' => 'Christmas');
?>
Easy so far...the problemis that I need a way of working out if todays date falls between the start date and end date, then changing the image file name.
Its a long shot but I hope someone would be kind enough to help.
Thanks,
B.
like this
$events = array(
'New Year' => '01/01 01/08',
'Pancake Day' => '16/02/2010 17/02/2010',
//etc
);
echo find_event($events, '16/02');
where find_event() is
function mdy2time($date) {
$e = explode('/', $date);
if(count($e) < 3)
$e[] = '2010';
return strtotime("$e[1]-$e[0]-$e[2]");
}
function find_event($events, $date = null) {
$date = is_null($date) ? time() : mdy2time($date);
foreach($events as $name => $range) {
list($start, $end) = explode(' ', $range);
if($date >= mdy2time($start) && $date <= mdy2time($end))
return $name;
}
return null;
}
you should use an array more like this:
$dates = array();
$dates[] = array(
'name' => 'New Years'
'start' = '01/14',
'end' => '01/20',
'style' => 'haloween',
);
$dates[] = array(
//...
);
then you can get the style as follows:
$style='default';
// date as number e.g. 130 (january 30th)
$currDate = date('md',time()) * 1;
foreach ($dates AS $k => $v) {
$tmp = explode("/",$v['start'];
$start = ($tmp[1].$tmp[0])*1;
$tmp = explode("/",$v['end'];
$stop = ($tmp[1].$tmp[0])*1;
if ($start <= $currDate && $currDate < $stop) {
$style=$v['style'];
break;
}
}
echo 'style: '.$style;
Didn't check the code yet, so feel free to correct me if iam wrong.

Categories