PHP dizi soru

3 Cevap

Nasıl PHP kullanarak dizideki adının yanındaki tarih sayı biçimini gösteriyor? Sayı biçimi veritabanına kaydedilir ve ay adı görüntülenir böylece?

İşte php kodudur.

$month_options = array("Month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

3 Cevap

Lütfen ayki sayı değişken içinde saklanır ise, sizin dizideki doğru öğeye erişmek için kullanabilirsiniz. Gibi $array[index] gibi bir sözdizimi kullanarak.


For example, the following portion of code :

$month_options = array("Month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$month_num = 3;

echo $month_options[$month_num];

size vermek istiyorum:

March

Unutmayınız ki, PHP, array indexes start at 0 - indeksi ile madde 3 dizide aslında dördüncü madde olduğu anlamına gelir.


Here, though, the first item in the array is not quite useful : it's a month -- so, you could remove it -- and you'd have to add 1 to the index used to point to the right month :

$month_options = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$month_num = 3;

echo $month_options[$month_num + 1];


And you might to go through the array section of the PHP manual ;-)

Ne yapmaya çalışıyorsunuz anlamak ise bu yardımcı olacaktır:

$month_options = array( 1 => "January", 
                        2 => "February",
                          ... snip ...
                        12 => "December");

echo $month_options[2]; // echoes February

Eğer veritabanı yerli tarih türünü kullanarak yapıyorsanız, muhtemelen '2010-04-07' gibi bir dize geri alıyoruz. , O bir zaman damgası almak strtotime() . That timestamp can then be passed to the date() function. The F biçiminde karakter $month_options dizi kullanmak zorunda kalmadan, size ay adını verir kullanmak için .

$when = get_date_field_from_database();
$timestamp = strtotime($when);
$month = date('F', $timestamp);