1

My code is something like this and we are facing problem when we use date in hindi and while saving to database it also throwing error and its not readable in php. We have changed it by Locale php standard function

$array = [$date1, $date2, date('Y-m-d', strtotime($date2))]; print_r($array);

Array
(
 [0] => 2014-08-10
 [1] => 2018-12-26
 [2] => 2018-12-26
)
Array
(
 [0] => 2019-01-31
 [1] => 30-जनवरी-2019  //We are using hindi 
 [2] => 1970-01-01  //Throwing wrong data it should be 2019-01-30
)

Warning : date_diff() expects parameter 2 to be DateTimeInterface, boolean given
Notice: Trying to get property of non-object
Newton Singh
  • 332
  • 3
  • 9
  • map the hindi names to english equivalents and then store the data I believe – Satya Feb 01 '19 at 05:41
  • yes @Satya i agree with your suggestion and i thought already for it but i need some fancy and standard way to do it. If you will help great Thanks – Newton Singh Feb 01 '19 at 05:44
  • can update the value in db also – Mohammad Malek Feb 01 '19 at 05:46
  • Is string replace works from ´जनवरी´ to ´January´ ? – Niklesh Raut Feb 01 '19 at 05:47
  • No we dont want to save date in hindi, we want to show date in hindi and when anyone do perform any action its just convert as php and mysql compatible format programatically without any intimation to user like 2019-01-30 – Newton Singh Feb 01 '19 at 05:49
  • @C2486 Yes it can be but i need some more stable way to do it – Newton Singh Feb 01 '19 at 05:51
  • no store in hindi date not found in this date library so you get the reset date of "01-01-70" – Mohammad Malek Feb 01 '19 at 05:55
  • 1
    @NewtonSingh, put the Hindi months in one array and English months in another . Before storing just take the English Equivalent of the month , create a new Date Object with it and you should be through. The array will come in handy when you are showing the date to user itself , so better put it in an Class and just use it – Satya Feb 01 '19 at 06:19
  • @Satya yes you are right and we have already did this way but we need some standard way to do it. Is it possible ? – Newton Singh Feb 01 '19 at 12:39
  • @NewtonSingh, that's the standard way, else , pull google translate/babelfish api to convert back and forth – Satya Feb 02 '19 at 00:32

2 Answers2

0

Try this:-

$date = "30-जनवरी-2019";

$my_date = date('m/d/y', strtotime($date));

echo $my_date;

Shehbaz Badi
  • 19
  • 1
  • 3
0

You can set locale and use Hindi in date function

setlocale(LC_ALL, "hi_IN"); // locale for Hindi (India), You can reset locale as it is later

Now,

$date2 = "30-जनवरी-2019";
echo date('Y-m-d', strtotime($date2));

setlocale(LC_ALL, "en_US");  //  "English (United States)" you can reset after use
Gautam Rai
  • 2,445
  • 2
  • 21
  • 31