Is there a fast way to convert date to spanish? - php

With this:
date( 'd F Y', strtotime( $row["datestart"] ) )
I get this:
08 July 2016
But I need to get this:
08 Julio 2016
Julio is July in spanish.
I have added this to the top of the php page:
setlocale(LC_TIME, 'es_ES');
but it doesn't work.
So what can I do ?

This worked for me:
setlocale(LC_TIME, 'es_ES', 'Spanish_Spain', 'Spanish');
$date = str_replace("/","-","08/07/2016");
echo strftime('%d %B %Y',strtotime($date)); // 08 julio 2016
setlocale is the key ingredient here.
update: PHP 8.x
$format = new IntlDateFormatter('es_ES', IntlDateFormatter::SHORT, IntlDateFormatter::NONE, NULL, NULL, 'dd MMMM y');
echo $format->format(new DateTime('now', new DateTimeZone('UTC')));
Use format to output the DateTime to the locale you specify in IntlDateFormatter (DateTimeZone is optional).

Another variant you may use:
Install intl extension for php (link).
Enable it in your php.ini file and then you will be able to check it is working with the following sample:
$f = new IntlDateFormatter('es_ES', null, null, null, null, null, 'dd MMMM y');
print($f->format(new DateTime('2016-07-08'));
An expected output will be the following:
08 julio 2016

You could make an associative array.
Set the keys to the months in English and the values to the corresponding months in Spanish.
It would look something like this...
$months = array(
'january' => 'enero',
'february' => 'febrero',
'march' => 'marzo',
'april' => 'abril',
'may' => 'mayo',
'june' => 'junio',
'july' -> 'julio',
'august' => 'agosto',
'september' => 'septiembre',
'october' => 'octubre',
'november' => 'noviembre',
'december' => 'diciembre'
);
Then you could reference the months like this...
$enMonth = "july"; //This is the month in English that you will match to the corresponding month in Spanish.
$esMonth = $months[$enMonth]; //You are returning the value (which is Spanish) of the key (which is English), giving you the month in Spanish.
You could probably also use Google Translate's API, but it seems like too much for something that can be done with a simple array.
Here is Google Translate's API if you are interested in translating other words, or a larger array of words.

Related

Show proper date before 1970

I am inserting selected DOB into database in "yyyy-mm-dd" formate using jquery date-picker. But when I am selecting DOB before 1970 then it gives me wrong DOB. eg. we select "August 19, 1949" then it show "August 19, 2049" in future year that is wrong. We are showing DOB in front-end using below mentioned code :
echo $newDate = date("M dS, Y", strtotime($BirthDetails['date']));
So please help me !!!
I'm using cakephp 3.0
When I print $BirthDetails['date'], it gives me
Cake\I18n\FrozenDate Object
(
[time] => 2011-08-19T00:00:00+00:00
[timezone] => UTC
[fixedNowTime] =>
)
Try this, use createFromFormat
// pass your date format
$date = DateTime::createFromFormat('d M Y','17 Jan 1949');
echo $date->format('Y-m-d');
DEMO
After a long period of searching, I found out my answer. We just need to do some changes in config/app.php file under the 'App' => []
replace
'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_US'),
to
'defaultLocale' => env('APP_DEFAULT_LOCALE', 'pl_PL'),
this is proper working in my scenario

Converting birthday in format DDMMMYY to date value in PHP

I've got bunch of birthdays which are stored in format DDMMMYY. I need to convert those to date values, so i can store those in database.
Is there any easy way of telling strtotime function that date must be in the past?
<?php
$datestring = '22SEP41';
echo date('Y-m-d',strtotime($datestring)); //outputs 2041-09-22, should be 1941-09-22
?>
<?php
$datestring = '22SEP41';
$matches = [];
preg_match('/([0-9]{2})([A-Z]{3})([0-9]{2})/', $datestring, $matches);
$prefix = ($matches[3] <= date('y') ? '20' : '19');
$matches[3] = "{$prefix}{$matches[3]}";
$ts = strtotime("{$matches[1]} {$matches[2]} {$matches[3]}");
// date ('Y-m-d', $ts) == 1941-09-22
This assumes that 22SEP06 should be interpreted as 2006 rather than 1906 - basically it gives the output a range of 1917 -> 2016.
This method create a date of past century only if standard evaluated date is after today:
$date = date_create( $datestring );
if( $date->diff( date_create() )->invert )
{
$date->modify( '-100 years' );
}
echo $date->format( 'Y-m-d' );
For
$datestring = '22SEP41';
the output is:
1941-09-22
For
$datestring = '22SEP01';
the output is:
2001-09-22
eval.in demo
Basically, we create a DateTime based on given string, then we calculate difference with current day; if the difference is negative (->invert), we subtract 1 century from the date.
You can personalize the condition using ->format('%R%Y') instead of ->invert. In this example:
if( $date->diff( date_create() )->format('%R%Y') < 10 )
Dates from 00 through 05 as evaluated as 2000-2005.
You could try something like:
$ds = '22SEP41';
$day = $ds[0].$ds[1];
// Getting the month.
$mon = array(
'JAN' => 1,
'FEB' => 2,
'MAR' => 3,
'APR' => 4,
'MAY' => 5,
'JUN' => 6,
'JUL' => 7,
'AUG' => 8,
'SEP' => 9,
'OCT' => 10,
'NOV' => 11,
'DEC' => 12
);
$mont = $ds[2].$ds[3].$ds[4];
$month = $mon[$mont]; // Gets the month real.
$year = '19'.$ds[5].$ds[6];
$final = $day.'-'.$month.'-'.$year;
I tested it on my local machine and it worked. Hope it works and is what you're looking for :)

Show date with custom month name, php

Im new to php and not sure how to operate with the string or date. I have custom month names, stored in array:
$months = ['1' => 'my Jan', '2'=>'my Feb', '3'=>'my Mar', '4'=>'my Apr', //etc.];
I'd like to show the date with these month names, currently I show:
date_format(new DateTime($route->start_date), 'd F Y')
which gives 01 January 2016
I need to get 01 my Jan 2016.
Thanks
hope this will help.
<?php
$months = ['1' => 'my Jan', '2'=>'my Feb', '3'=>'my Mar', '4'=>'my Apr'];
$date = new DateTime($route->start_date);
echo '<br>'.$date->format('d').' '.$months[$date->format('n')].' '.$date->format('Y');
?>

WordPress localisation of german date string

Hi #ll and greetings from Germany,
Perhaps I am too blind to see, but I am struggling with a localisation problem. I hope that someone has a solution for that.
In a function I have a german date string, assume it's today:
$datestring = '3. März 2014'
Second, i have the date format used in WP
$date_format = get_option( 'date_format' );
What I am trying to achive is to get a valid unix timestamp from $datestring.
I tried several different approaches like strtotime, date_parse_from_format, etc., I even tried to set the locale environment via setlocale. Of course it would be easy to parse the string against an array with the month names and get an english datestring, but I want to have this ready for all languages.
Any Idea how to get this done? Help is really appreciated.
You need to use mktime($datestring)
and set the $datestring variable in accordance to:
http://pl1.php.net/manual/de/function.mktime.php
in wordpress:
mktime(get_the_date("H"), get_the_date("i"), get_the_date("s"), get_the_date("n"), get_the_date("j"), get_the_date("Y"));
if you need to convert exact format shown above:
$datestring = '3. März 2014';
$dateelements = explode(" ", $datestring);
$day = rtrim($dateelements[0], ".");
$germanMonths = array(1 => "Januar", 2 => "Februar", 3 => "März", 4 => "April", 5 => "Mai", 6 => "Juni", 7 => "Juli", 8 => "August", 9 => "September", 10 => "Oktober " , 11 => "November", 12 => "Dezember");
$month = array_search($dateelements[1], $germanMonths);
$year = $dateelements[2];
$unixtimestamp = mktime(0,0,0,$month,$day,$year);
and if you have a date format in wp option, and $datestring is in that format:
$dateformat = get_option('date_format');
echo $dateformat;
$date = date_create_from_format($dateformat, $datestring);
$new_format = date_format($date, 'm,d,Y');
$unixtimestamp = mktime(0,0,0,$new_format);
echo $unixtimestamp;

How to display PHP date in Turkish?

How can I display the date in Turkish language?
I'm trying following code but it does not print anything at all.
setlocale(LC_ALL, 'tr_TR.UTF-8');
echo strftime("%e %B %Y %A", time());
Though i don't understand turkish but it is printing output
14 Şubat 2013 Perşembe
So your code is fine.
Hope you are not missing out php tags. :/
<?php
setlocale(LC_ALL, 'tr_TR.UTF-8');
echo strftime("%e %B %Y %A", time());
?>
Prints out: 30 Ekim 2015 Cuma
Code works fine.
Php date for "F" which gives month of English name to translate to Turkish given example
date("d F")
you can use this function
function convertMonthToTurkishCharacter($date){
$aylar = array(
'January' => 'Ocak',
'February' => 'Şubat',
'March' => 'Mart',
'April' => 'Nisan',
'May' => 'Mayıs',
'June' => 'Haziran',
'July' => 'Temmuz',
'August' => 'Ağustos',
'September' => 'Eylül',
'October' => 'Ekim',
'November' => 'Kasım',
'December' => 'Aralık',
'Monday' => 'Pazartesi',
'Tuesday' => 'Salı',
'Wednesday' => 'Çarşamba',
'Thursday' => 'Perşembe',
'Friday' => 'Cuma',
'Saturday' => 'Cumartesi',
'Sunday' => 'Pazar',
'Jan' => 'Oca',
'Feb' => 'Şub',
'Mar' => 'Mar',
'Apr' => 'Nis',
'May' => 'May',
'Jun' => 'Haz',
'Jul' => 'Tem',
'Aug' => 'Ağu',
'Sep' => 'Eyl',
'Oct' => 'Eki',
'Nov' => 'Kas',
'Dec' => 'Ara'
);
return strtr($date, $aylar);
}
add it before the code.
setlocale (LC_ALL, 'tr_TR.UTF-8', 'tr_TR', 'tr', 'turkish');
public function getTurkishDate(){
$locale = 'tr_TR'; // a canonicalized locale
$format = 'dd-MMMM-YYYY'; // ISO format codes, not the typical date ones
$dt = new DateTime(); // a DateTime object
$df = new IntlDateFormatter(
$locale, // string locale
IntlDateFormatter::NONE, // int date type
IntlDateFormatter::NONE, // int time type
'UTC', // string timezone
IntlDateFormatter::GREGORIAN, // int cal type
$format // string pattern
);
return $df->format($dt); //string 07-Ağustos-2018
}
OR
public function getTurkishDate(){
$locale = 'tr_TR'; // a canonicalized locale
$format = 'dd-MMMM-YYYY-EEEE'; // ISO format codes, not the typical date ones
$dt = new DateTime(); // a DateTime object
$df = new IntlDateFormatter(
$locale, // string locale
IntlDateFormatter::NONE, // int date type
IntlDateFormatter::NONE, // int time type
'UTC', // string timezone
IntlDateFormatter::GREGORIAN, // int cal type
$format // string pattern);
return $df->format($dt); //string 07-Ağustos-2018-Salı
}
OR
public function getTurkishDate(){
$locale = 'tr_TR'; // a canonicalized locale
$format = 'dd-MMMM-YYYY-ee'; // ISO format codes, not the typical date ones
$dt = new DateTime(); // a DateTime object
$df = new IntlDateFormatter(
$locale, // string locale
IntlDateFormatter::NONE, // int date type
IntlDateFormatter::NONE, // int time type
'UTC', // string timezone
IntlDateFormatter::GREGORIAN, // int cal type
$format // string pattern);
return $df->format($dt); //string 07-Ağustos-2018-02
}
You can take current date and time with:
<?php
date_default_timezone_set("Europe/Istanbul");
//echo "The time is " . date("H:i") . "<br>";
//echo "Today is " . date("Y-m-d") . "<br>";
//echo "Today is " . date("l");
$now_time = date("H:i");
$now_date = date("Y-m-d");
?>

Categories