0

I am very new to PHP(5.x) and MySQL ,

I am getting some information from MySQL in my PhP app. Which includes Dates.

I have a full date like 01.01.2016 09:30 . How can I show this in my View(HTML) Table as January 2016 ?

I am displaying it now like that

       foreach($data as $d) {
            echo "<tr>";
            echo "<td style='white-space: nowrap'>".$d["DatumVon"]."h</td>";
            echo "<td style='white-space: nowrap'>".$d["DatumBis"]."h</td>";
            echo "<td>".$d["dauerStunden"]."</td>";
            echo "</tr>";
        }

The date format I show is dd.mm.yyyy but the data is stored on the database as a TIMESTAMP

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Look at http://php.net/manual/en/function.strtotime.php and http://php.net/manual/en/function.date.php Also `01.01.2016 09:30` is not a valid date/datetime structure your DB should be using one of those formats to store dates. – chris85 Aug 04 '17 at 11:09
  • 1
    MySQL DATE/DATETIME columns are not stored like this `01.01.2016 09:30` What datatype is the column storing this date – RiggsFolly Aug 04 '17 at 11:10
  • 1
    _QUESTION:_ You have `01.01` for day and month. Which one is Day and which one is Month??? **It matters** – RiggsFolly Aug 04 '17 at 11:12
  • first one is for day , second one is for month, also European style not American –  Aug 04 '17 at 11:21
  • Also I have time stemps in mysql like 1365061500 it will be "translated" automatically to human dates –  Aug 04 '17 at 11:22
  • @AtlasOkyanus check my answer below hope it would help – B. Desai Aug 04 '17 at 11:28

3 Answers3

0

You can use php date function

$get_date="01.01.2016 09:30";
echo date("F Y",strtotime($get_date)); //op January 2016

EDIT

If you have timestamp as you have mentioned in comment. You can directly pass that in date function

$time_stamp = "1365061500";
echo date("F Y",$time_stamp); 
B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • 2
    Ok, but do you know which `01` is Day and which is Month yet? – RiggsFolly Aug 04 '17 at 11:14
  • yes @RiggsFolly it must be `dd.mm.yyyy` format as in many countries it is valid format https://en.wikipedia.org/wiki/Date_format_by_country – B. Desai Aug 04 '17 at 11:22
  • But in many others it is written as `mm.dd.yyyy` and as that is NOT a valid MySQL `DATE` or `DATETIME` format. My guess is OP has stored the date in a `VARCHAR()` so it could be anything. And if it is `mm.dd.yyyy` than your code will not generate the correct answer – RiggsFolly Aug 04 '17 at 11:25
  • Will you show me where `mm.dd.yyyy` is valid date. btw OP already mentioned that its on `dd.mm.yyyy` format – B. Desai Aug 04 '17 at 11:27
  • Yes now he has mentioned the format, and that it is a timestamp on the database. `mm.dd.yyyy` is used all the time in the USA – RiggsFolly Aug 04 '17 at 11:30
  • I think it is `mm/dd/yyyy` – B. Desai Aug 04 '17 at 11:32
  • The seperator is actually quite irrelevant, the position is what is important – RiggsFolly Aug 04 '17 at 11:36
  • separator is also imp if you will get result of "02/01/2016 09:30" it will be `1st February 2016` but for "02.01.2016 09:30" it will be `2nd January 2016` :) – B. Desai Aug 04 '17 at 11:40
  • Only as related to the PHP date functions. I was suggesting people clarify the information that the OP supplied before jumping into an answer as suggested in https://meta.stackoverflow.com/questions/308302/how-to-deal-with-unclear-questions-and-their-lightning-fast-fastest-gun-in-the – RiggsFolly Aug 04 '17 at 11:44
  • Specifically paragraph 4 of this answer https://meta.stackoverflow.com/a/308358/2310830 – RiggsFolly Aug 04 '17 at 11:48
0
<?php
$originalDate = "01.01.2016 09:30";
echo $newDate = date("jS F Y", strtotime($originalDate));

$originalDate = "01.01.2016 09:30";
echo $newDate = date("F Y", strtotime($originalDate));

?>

Output : 1st January 2016 Output : January 2016

Naushil Jain
  • 434
  • 3
  • 11
0

strtotime : function an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

getdate : Retourne la date and heure from date

You can try:

$date = getdate(strtotime($d["DatumVon"]));
$monthYearDate = $date["month"]."&nbsp;".$date["year"];
Mohamed Baddi
  • 13
  • 1
  • 5