Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date('now') in PHP

Tags:

php

After a long time I needed to use date function of PHP. I wrote something like:

echo date('now');

and I got the output below:

1220123

What does that mean ?

like image 612
Dhaval Avatar asked Dec 19 '12 06:12

Dhaval


People also ask

What does NOW () return in PHP?

It is a PHP function that "returns the date & time in the same format as the MySQL function NOW()", which is precisely the question.

How can I get current date in PHP?

You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.

What does date () do in PHP?

The date/time functions allow you to get the date and time from the server where your PHP script runs. You can then use the date/time functions to format the date and time in several ways. Note: These functions depend on the locale settings of your server.

How can I get current date in dd mm yyyy format in PHP?

$today = date('d-m-y'); to $today = date('dd-mm-yyyy');


2 Answers

From the PHP manual :

  • n Numeric representation of a month, without leading zeros

  • o ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)

  • w Numeric representation of the day of the week

So, date("now") displays 12 (n), 2012 (o) and 3 (w).

You're probably looking for :

  • date("Y-m-d") for a date

  • date("Y-m-d H:i:s") for a datetime

like image 186
Alain Tiemblo Avatar answered Oct 30 '22 20:10

Alain Tiemblo


"now" is not a valid parameter for for this expectation, infact it should be strtotime function here, not date.

Date considers your now as

n

Numeric representation of a month, without leading zeros

o

ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)

w

Numeric representation of the day of the week

like image 20
Hanky Panky Avatar answered Oct 30 '22 20:10

Hanky Panky