-1

I am trying to get current time and date in order to echo it out on my website. I have the follow snippet:

$date_of_msg  = date("Y-m-d");
$time_of_msg  = time();

When I echo $time_of_msg I get 00:00:00. I have tried to edit my code based on this solution here but which this approach, when I echo the variable, I get 838:59:59. I simply want the current time to be displayed in 24 hour format.

In addition to this, I currently have the date formatted to (Y-m-d), which is great because it works. I am trying to format it so that it displays day, number, year, i.e. today is 20th Feb, so I want the date to display Feb 20, 2016. I have tried the following based on documentation (see here)

$date_of_msg = date("F j, Y")

But again, the date displays nothing. Am I missing something?

Community
  • 1
  • 1
Freddy
  • 683
  • 4
  • 35
  • 114
  • 1
    `echo $time_of_msg = time();` => 1455927303 is what I get back. What am I missing here? Or, should I say: what is missing in this question and that you're not telling us? Oh, the mysql stuff. check your column type. – Funk Forty Niner Feb 20 '16 at 00:15
  • 1
    From one Fred to another; your question's unclear. – Funk Forty Niner Feb 20 '16 at 00:26

4 Answers4

1

time() returns a UNIX timestamp while date() format a timestamp. Your call date("Y-m-d") means the same as date("Y-m-d", time()).

Even though the function is called date(), it can also format time. You just have to use the correct placeholders. E.g. date("H:i:s") would give you a 24h-time like 17:43:23.

jsfan
  • 1,373
  • 9
  • 21
1

time() (unless you override it in some weird fashion) gives you a timestamp, i.e. the amount of seconds which have passed since 1970-01-01 until now. date(), however, gives you a string representation of a date, which may or may not include the minutes and seconds - depending on how you choose to format it.

So, if you want to display the time and date to a user, you should probably go for something like

$date_of_msg = date("F j, Y H:i:s")

The documentation on date() gives you an excellent description of available options.

Sergey Vidusov
  • 1,342
  • 1
  • 7
  • 10
1

If you need the current time in the 24h format just use

$time_of_msg = date("H:i");

The date part seems correct that way, you must be doing something wrong while displaying it.

phaberest
  • 3,140
  • 3
  • 32
  • 40
0

This code

<?php
echo date("Y-m-d").PHP_EOL;
echo time().PHP_EOL;
echo date("F j, Y").PHP_EOL;

Returns this result, as expected

2016-02-20
1455927480
February 20, 2016

So what are you doing that you are not actually telling us

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149