Created
July 31, 2013 11:26
-
-
Save amin007/6121237 to your computer and use it in GitHub Desktop.
dari string kepada date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Katakan data berbentuk seperti ini | |
F0003=26031966 | |
F0004=1012010 | |
F0005=31122010 | |
F0003, F0004, F0005 adalah nama medan | |
cadangnya nak pecahkan jadi seperti ini | |
F0003=26-03-1966 / 26 Mac 1966 | |
F0004=1-01-2010 / 1 Jan 2010 | |
F0005=31-12-2010 / 31 Dis 2010 | |
Cara pertama | |
<?php | |
echo $var = "1122010"; | |
echo '<br>dd-mm-yyyy=' . substr($var,8,2) . '' | |
. substr($var,6,2) . '' | |
. substr($var,-4); | |
echo '<br>'; | |
echo $var = "31122010"; | |
echo '<br>dd-mm-yyyy=' . substr($var,8,2) . '' | |
. substr($var,6,2) . '' | |
. substr($var,-4); | |
?> | |
output dia | |
1122010 =>dd-mm-yyyy=11-12-2010 | |
31122010 =>dd-mm-yyyy=31-12-2010 | |
ikutkan 1122012 ada 7 digit kalau boleh nak buat dd-mm-yyyy=1-12-2010 | |
Cara kedua - disyorkan oleh Ikhwan Hayat | |
<?php | |
$input = "1122010"; | |
$len = strlen($input); | |
if ($len == 6) { $dayLen = 1; $monthLen = 1; $monthStart = 1; } | |
else if ($len == 7) {$dayLen = 1;$monthLen = 2;$monthStart = 1;} | |
else if ($len == 8) {$dayLen = 2;$monthLen = 2;$monthStart = 1;} | |
else { /*throw exception */ } | |
echo "$input=" . substr($input, 0, $dayLen) | |
. " - " . substr($input, $monthStart, $monthLen) | |
. " - " . substr($input, -4); | |
$input = "31122010"; | |
$len = strlen($input); | |
if ($len == 6) { $dayLen = 1; $monthLen = 1; $monthStart = 1; } | |
else if ($len == 7) {$dayLen = 1;$monthLen = 2;$monthStart = 1;} | |
else if ($len == 8) {$dayLen = 2;$monthLen = 2;$monthStart = 1;} | |
else { /*throw exception */ } | |
echo "<br>$input=" . substr($input, 0, $dayLen) | |
. " - " . substr($input, $monthStart, $monthLen) | |
. " - " . substr($input, -4); | |
?> | |
Output | |
1122010=1 - 12 - 2010 | |
31122010=31 - 11 - 2010 | |
Cara ketiga - disyorkan oleh Ang Chin Han | |
<?php | |
function parseDate($d) | |
{ | |
if (preg_match('/(\d{1,2})(\d{2})(\d{4})/', $d, $matches)) | |
{ | |
@list(,$d, $m, $y) = $matches; | |
return array($y, $m, $d); | |
} | |
return; | |
} | |
echo '<pre>'; | |
print_r(parseDate('1122012')); | |
print_r(parseDate('31122010')); | |
echo '</pre>'; | |
?> | |
Output | |
Array | |
( | |
[0] => 2012 | |
[1] => 12 | |
[2] => 1 | |
) | |
Array | |
( | |
[0] => 2010 | |
[1] => 12 | |
[2] => 31 | |
) | |
Cara keempat - disyorkan oleh Jasdy Syarman | |
<?php | |
date_default_timezone_set('Asia/Kuala_Lumpur'); | |
$str = array( | |
'F0003 26031966', | |
'F0004 1012010', | |
'F0005 31122010' | |
); | |
foreach ($str as $key) | |
{ | |
$split_value = explode(' ',$key); | |
$day = substr($split_value[1],0,2); | |
$month = substr($split_value[1],2,2); | |
$year = substr($split_value[1],4,4); | |
$massage_date = $year.'-'.$month.'-'.$day; | |
$display_date = date('d M Y',strtotime($massage_date)); | |
echo $split_value[0] .' '.$day.'-'.$month.'-'.$year.' / '.$display_date.'<br />'; | |
} | |
?> | |
Output | |
F0003 26-03-1966 / 26 Mar 1966 | |
F0004 10-12-010 / 10 Dec 2010 | |
F0005 31-12-2010 / 31 Dec 2010 | |
Cara kelima - disyorkan oleh Mior Muhammad Zaki | |
Kod asal => http://paste.laravel.com/FfZ | |
<?php | |
$strings = ["F0003 26031966", "F0004 1012010", "F0005 31122010"]; | |
foreach ($strings as $str) | |
{ | |
echo preg_replace('/^(F\d{4})\s(\d{1,2})(\d{2})(\d{4})$/', '$1 $2-$3-$4', $str).PHP_EOL; | |
} | |
?> | |
Output | |
F0003 26-03-1966 F0004 1-01-2010 F0005 31-12-2010 | |
Cara ke enam - disyorkan oleh Noorzaini Ilhami Mohd Noor | |
<?php | |
function parseDate2($d) { | |
$d = strlen($d) < 8 ? "0$d" : $d; | |
return date_parse_from_format('jmY', $d); | |
} | |
echo '<pre>'; | |
print_r(parseDate2("1122012")); | |
print_r(parseDate2("31122010")); | |
echo '</pre>'; | |
?> | |
Output | |
Array | |
( | |
[year] => 2012 | |
[month] => 12 | |
[day] => 1 | |
[hour] => | |
[minute] => | |
[second] => | |
[fraction] => | |
[warning_count] => 0 | |
[warnings] => Array | |
( | |
) | |
[error_count] => 0 | |
[errors] => Array | |
( | |
) | |
[is_localtime] => | |
) | |
Array | |
( | |
[year] => 2010 | |
[month] => 12 | |
[day] => 31 | |
[hour] => | |
[minute] => | |
[second] => | |
[fraction] => | |
[warning_count] => 0 | |
[warnings] => Array | |
( | |
) | |
[error_count] => 0 | |
[errors] => Array | |
( | |
) | |
[is_localtime] => | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment