0

i make a php page here my dates are assign to label of fusion chart how to convert date format from 01-02-2014 to Jan 2? HERE IS MY CODE:

$timestamp = time();
for ($i = 0 ; $i <=30 ; $i++) {
   $dates[$i]= date('m-d-Y', $timestamp);
    $timestamp -= 24 * 3600;
}
Muhammad Arif
  • 1,014
  • 3
  • 22
  • 56
  • What? You are creating an array of dates in this code, what exactly is your question? How to convert one of those, each, some? You can use `strtotime()` to get a timestamp from a format and `date()` to get the needed output. Also PHP offers a great `DateTime` class which could be used for this – kero Feb 01 '14 at 13:51

3 Answers3

1

This will help you further

<?php
$timestamp = time();
for ($i = 0 ; $i <=30 ; $i++) {
   $dates[$i]= date('M j', $timestamp);
    $timestamp -= 24 * 3600;
}

print_r($dates);

Array
(
    [0] => Feb 1
    [1] => Jan 31
    [2] => Jan 30
    [3] => Jan 29
    [4] => Jan 28
    [5] => Jan 27
    [6] => Jan 26
    [7] => Jan 25
    [8] => Jan 24
    [9] => Jan 23
    [10] => Jan 22
    [11] => Jan 21
    [12] => Jan 20
    [13] => Jan 19
    [14] => Jan 18
    [15] => Jan 17
    [16] => Jan 16
    [17] => Jan 15
    [18] => Jan 14
    [19] => Jan 13
    [20] => Jan 12
    [21] => Jan 11
    [22] => Jan 10
    [23] => Jan 9
    [24] => Jan 8
    [25] => Jan 7
    [26] => Jan 6
    [27] => Jan 5
    [28] => Jan 4
    [29] => Jan 3
    [30] => Jan 2
)
Sundar
  • 4,580
  • 6
  • 35
  • 61
0

You can see all the formatting options at php.net/manual/en/function.date.php

 $dates[$i]= date('d M', $timestamp);

Output :

Array
(
    [0] => 01 Feb
    [1] => 31 Jan
    [2] => 30 Jan
    [3] => 29 Jan
    [4] => 28 Jan
    .............
)
gskema
  • 3,141
  • 2
  • 20
  • 39
0

Use DateTime::setTimestamp

<?php

$timestamp = time();
for ($i = 0 ; $i <=30 ; $i++) {
$date = new DateTime();
$date->setTimestamp($timestamp);
 $dates[$i]=$date->format('M d') . "\n";
 $timestamp -= 24 * 3600;
}

print_r($dates);

output:

 Array
    (
        [0] => Feb 01

        [1] => Jan 31

        [2] => Jan 30

     ....
)

Demo

Nambi
  • 11,944
  • 3
  • 37
  • 49