xfire123 Posted October 20, 2017 Share Posted October 20, 2017 Hello im trying to search this kind of parse. I have this string in variable: $a = "май 2017"; How to parse to for example: "5 2017" or "5 17" or "5" I can parse it from EN text but with cyrillic text i cant... Please help b.t.w i cant use array to store all cyrillic months because they coming from different script. Link to comment Share on other sites More sharing options...
requinix Posted October 20, 2017 Share Posted October 20, 2017 Set the locale correctly and strptime or strtotime should work. Link to comment Share on other sites More sharing options...
xfire123 Posted October 20, 2017 Author Share Posted October 20, 2017 Set the locale correctly and strptime or strtotime should work. This is my code: if $filename = "Май 2017" <?php $dir_open = opendir('./WEBDams/Data/CHQ/HQ/1/'); while(false !== ($filename = readdir($dir_open))){ if($filename != "." && $filename != ".."){ setlocale(LC_TIME, 'bg_BG.UTF-8','bulgarian'); $filenameNOext = basename($filename, '.pdf'); $filenameNOext2 = strftime($filenameNOext); $filenameNOext3 = preg_replace('/\d/', '', $filenameNOext2); $filenameNOext4 = preg_replace('/\s+/', '', $filenameNOext3); $link = "<li class='sort-item' data-event-date='$filenameNOext4'><a href='./WEBDams/Data/CHQ/HQ/1/$filename' ><strong> $filenameNOext г. $filenameNOext3</strong></a></li>"; echo $link; } } closedir($dir_open); Im using it with this script: (function($){ var container = $(".sort-list"); var items = $(".sort-item"); items.each(function() { // Convert the string in 'data-event-date' attribute to a more // standardized date format var BCDate = $(this).attr("data-event-date"); var standardDate = BCDate[0]; standardDate = new Date(standardDate).getTime(); $(this).attr("data-event-date", standardDate); }); items.sort(function(a,b){ a = parseFloat($(a).attr("data-event-date")); b = parseFloat($(b).attr("data-event-date")); return a>b ? -1 : a<b ? 1 : 0; }).each(function(){ container.prepend(this); }); })(jQuery); /* This script sorts your list in descending order... to change it to ascending order change the "less than" operator (<) to "greater than" (>) */ But its not working for some reason... can you give me an example? Link to comment Share on other sites More sharing options...
requinix Posted October 20, 2017 Share Posted October 20, 2017 I can get it to work but only with a lowercase month name. Note that you're calling strptime() incorrectly. How about IntlDateFormatter::parse (which will take me some time to get set up so I can't test it right now)? Link to comment Share on other sites More sharing options...
xfire123 Posted October 20, 2017 Author Share Posted October 20, 2017 I can get it to work but only with a lowercase month name. Note that you're calling strptime() incorrectly. How about IntlDateFormatter::parse (which will take me some time to get set up so I can't test it right now)? yes it can be with lowercase. I see the link but cant understand the method very well. Link to comment Share on other sites More sharing options...
requinix Posted October 20, 2017 Share Posted October 20, 2017 Try $formatter = new IntlDateFormatter("bg_BG.UTF-8", null, null, null, null, "MMM yyyy"); $dt = $formatter->parse("Май 2017"); echo date("Y-m-d", $dt);It does require you knowing what date format the filename uses, though. Link to comment Share on other sites More sharing options...
xfire123 Posted October 20, 2017 Author Share Posted October 20, 2017 It giving me: Fatal error: Uncaught Error: Class 'IntlDateFormatter' not found in C:\WEB\Apache24\htdocs\inc_req\menu_HQ.php:2 Stack trace: #0 C:\WEB\Apache24\htdocs\HQ.php(20): require() #1 {main} thrown in C:\WEB\Apache24\htdocs\inc_req\menu_HQ.php on line 2 I comment out extension=php_intl.dll restarted service. Still same error. Link to comment Share on other sites More sharing options...
requinix Posted October 20, 2017 Share Posted October 20, 2017 Comment out? No, you need that. Uncomment it and restart. Does var_dump(extension_loaded("intl"));show the extension is loaded? Does Apache's error log say anything useful? Link to comment Share on other sites More sharing options...
xfire123 Posted October 20, 2017 Author Share Posted October 20, 2017 its say: PHP Warning: PHP Startup: Unable to load dynamic library 'C:\\WEB\\PHP\\ext\\php_intl.dll' - The specified module could not be found.\r\n in Unknown on line 0 I fixed it when i copy all icu*.dll files in Apache bin directory... at last I tested it and works like a charm. Thank you very much. Link to comment Share on other sites More sharing options...
xfire123 Posted October 25, 2017 Author Share Posted October 25, 2017 Hello again. I'm wondering now how to fix this sort from: 10 01 02 03 04 05 06 07 08 09 to: 1 2 3 4 5 6 7 8 9 10 The main problem i think is the "0" infront Link to comment Share on other sites More sharing options...
xfire123 Posted October 25, 2017 Author Share Posted October 25, 2017 I used "n" instead of "m" in date. But the sorting in js its not working. Link to comment Share on other sites More sharing options...
Barand Posted October 25, 2017 Share Posted October 25, 2017 The correct sorting of dates requires Y-m-d format (yyyy-mm-dd). If you are sorting within your code and want to display another format then reformat on output, Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.