I'am trying to change dates locale.For example ive got date variable, 2015/Jun/01, 2015/Jun/19/ 2015/Mar/14 and etc. this variable called $startDate. Im trying to change english to my locale lets say lt_LT. Here is the code which i tried to use:
$startDate = ($start_date ? $start_date->format($df) : '-');
setlocale(LC_ALL, 'lt_LT.UTF-8');
echo strftime($startDate);
But it still prints dates in english, what am i doing wrong?
You need to specify output date format in strftime and also pass the $startDate as timestamp.
$startDate = "2015/Mar/14";
$startDate = strtotime(str_replace('/', '-', $startDate));
setlocale(LC_ALL, 'lt_LT.UTF-8');
echo strftime("%Y/%b/%d", $startDate);
Result
2015/Kov/14
More information about strftime on php.net
Asume, that lt_LT.UTF-8 is missing on your system, as it's working very well on my system. When using unknown locales, it will print in english too.
When using Debian, try:
dpkg-reconfigure locales
Related
I am bumping against a language barrier when I try to push out out meta data.
$string_date = implode (';', get_post_meta( get_the_ID(), 'dbt_opleverdatum' ) );
$format = get_option( 'date_format' );
$string_date = date($format, $string_date)
Since I save the meta data as a php time stamp, its being put away as a 1451865600 for example (which is january 4th 2016). I pick it up, and put it out via a date stamp which put it down for me as 4 January 2016
However, the whole site is run in Dutch and as I am looking through the code I notice the functions get_the_date() which retrieves the date of posting. But that will output 4 januari 2016 because that goes via the settings in Wordpress it self. It notices the wordpress is in Dutch and takes that in account.
Is there a function I can put my PHP date through that as well, so my date will be propperly shown in Dutch, instead of English?
From: http://php.net/manual/en/function.date.php:
To format dates in other languages, you should use the setlocale() and
strftime() functions instead of date().
$ts = 1451865600;
$lang = get_option('WPLANG');
setlocale(LC_TIME, $lang);
$format2 = '%e %B %G';
$string_date = strftime($format2, $ts);
Unfortunately I don't know how to convert the date_format in the options to the format string of strftime, but if it's your plugin, then you can change it to mean the format for strftime, or if you use it in other places as well, then add another option for this.
Notice: using setlocale changes the locale globally, so it might be necessary to revert the chage after the call to strftime:
$current_locale = setlocale(LC_TIME, "0");
setlocale(LC_TIME, $lang);
...
setlocale(LC_TIME, $current_locale);
I have a problem with verifying dates in PHP 5.2.17. I need the user to be able to enter the six digit date without any punctuation so Jan. 15, 2014 would be 011514. (This is due to input device limitation and for user convenience).
Tested and this is not working:
$testDate = "011514";
$myDate = date('mdy', strtotime($testDate));
echo $myDate;
I would expect that myDate is the same as test date but it's not, it prints out today's date 042314 (April 23, 2014)! PHP doesn't like that there is no punctuation in my original string. If I change $testDate = "01-15-14" then PHP spits out the correct string "011514".
How can I get strtotime() to understand that I want to check dates in 'mdy' format without using date_create_from_format ?
Extra info:
PHP version is 5.2.17 so date_create_from_format is NOT able to be used.
There's a limit to what strtotime() can understand. It can't understand everything. That's why they tell us what it does and we, as developers, need to write our code in a way that respects that.
All you need to do in your code is insert slashes and you're all set.
$testDate = "011514";
$date = sprintf('%s/%s/%s',
substr($testDate, 0, 2),
substr($testDate, 2, 2),
substr($testDate, 4, 2)
);
$myDate = date('mdy', strtotime($date));
echo $myDate;
Demo
Answering to your only question
How can I get strtotime() to understand that I want to check dates in 'mdy' format without using date_create_from_format ?
To get strtotime() to understand your format you need to download php sources, patch it so that it supported such a weird format and use your custom build.
i am storing date and time in database using php using gmdate() function in format "Y-m-d H:i:s". for e.g.
2014-03-10 12:35:55
Now,on getting this data into a php variable,for e.g.
$temp=2014-03-10 12:35:55
can i extract and display only the DATE portion excluding the TIME portion??
I am new to date and time manipulation.Any help?
you can get directly the date from database like this
select DATE(column_datetime) as date from yourtable
As Pramod answered,
date('Y-m-d', strtotime($temp));
works.
date('Y-m-d', strtotime($temp));
The above statement returns the date from datetime format
The following code example is adapted by this thread: How to convert date to timestamp in PHP?
There you can also read why it is not safe to rely on strtotime (answer by daremon)
$temp = '2014-03-10 12:35:55';
$a = strptime($temp, '%Y-%m-%d');
$timestamp = mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900);
Edit
From the PHP manual
Note: This function is not implemented on Windows platforms.
Try this:
Echo date('Y-M-D', $var);
I'm using this code: date("M j, Y", $end); to display date.
Problem is I've multilingual website, with wordpress + qtranslate.
Now M is problematic as it does not get translated. I read about setlocale php, anyone care explaining how this or if it would work at all?
Or maybe I should find some special qtrnaslate function that does this?
Wordpress has its own date formating function for international dates, date_i18n
From the manual on date()
http://php.net/manual/en/function.date.php
To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().
Duplicate
Managing date formats differences between PHP and MySQL
PHP/MySQL: Convert from YYYY-MM-DD to DD Month, YYYY?
Format DATETIME column using PHP after printing
date formatting in php
Dear All,
I have a PHP page where i wil be displaying some data from Mysql db.
I have 2 dates to display on this page.In my db table, Date 1 is in the format d/m/Y (ex: 11/11/2002) and Date 2 is in the format d-m-Y (ex : 11-11-2002)
I need to display both of this in the same format .The format i have stored in a variable $dateFormat='m/d/Y'
Can any one guide me
Thanks in advance
Use strtotime to convert the strings into a Unix timestamp, then use the date function to generate the correct output format.
Since you're using the UK date format "d/m/Y", and strtotime expects a US format, you need to convert it slighly differently:
$date1 = "28/04/2009";
$date2 = "28-04-2009";
function ukStrToTime($str) {
return strtotime(preg_replace("/^([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{1,4})/", "\\2/\\1/\\3", $str));
}
$date1 = date($dateFormat, ukStrToTime($date1));
$date2 = date($dateFormat, ukStrToTime($date2));
You should be all set with this:
echo date($dateFormat, strtotime($date1));
echo date($dateFormat, strtotime($date2));
You may want to look into the strptime function. This can convert any date from a string back into numeric values. Unlike strtotime, it can be adapted to different formats, including those from different locales, and its output is not a UNIX timestamp, so it's capable of parsing dates before 1970 and after 2037. It may be a little bit more work though because it returns an associative array though.
Unfortunately it's not available on Windows systems either so it's not portable.
If for some reason strtotime will not work for you, could always just replace the offending punctuation with str_replace.
function dateFormat($date) {
$newDate = str_replace(/, -, $date);
echo $newDate;
}
echo dateFormat($date1);
echo dateFormat($date2);
I know this will make most folks cringe, but it may help you with formatting non-date strings in the future.
rookie i am. so came up with the method that just do that. what mysql needs.. shish i used param 2... hope it helps. regards
public function dateConvert($date,$param){
if($param==1){
list($day,$month,$year)=split('[/.-]',$date);
$date="$year-$month-$day"; //changed this line
return $date;
}
if ($param == 2){ //output conversion
list($day,$month,$year) = split('[/.]', $date);
$date = "$year-$day-$month";
return $date;
}
}