How can i get current date in yyyy mm dd format in php?

I need to set a variable to the current date in this format: eg: 2012-05-12 , I get that's YYYY-MM-DD ?

I've tried:

$date = date("yyyy-mm-dd", strtotime(now)); 

but this is not saving the date to mysql so it's coming out as 0000-00-00.

The field is set as type: date in the mysql db.

What am I doing wrong here?

jprofitt

10.8k4 gold badges34 silver badges45 bronze badges

asked Nov 13, 2012 at 13:10

Satch3000Satch3000

45k86 gold badges210 silver badges344 bronze badges

5

answered Nov 13, 2012 at 13:13

Always check the documentation, and you’ll find the format you’ll need is:

<?php date('Y-m-d'); ?>

EDIT: Also, you don’t need to specify the time as the second parameter, as date() will use now by default. It should be noted that your syntax there is wrong too though, as it would be strtotime('now'), as “now“ is a string it needs to be quoted.

answered Nov 13, 2012 at 13:11

Martin BeanMartin Bean

37k24 gold badges121 silver badges195 bronze badges

use

$date = date("Y-m-d");

Then save the $date variable to the database

How can i get current date in yyyy mm dd format in php?

answered Nov 13, 2012 at 13:26

  • Home
  • PHP Home
  • PHP Function Reference
  • ▼Date Functions
  • checkdate
  • date_default_ timezone_get
  • date_default_ timezone_set
  • date_sunrise
  • date_sunset
  • date
  • getdate
  • gettimeofday
  • gmdate
  • gmmktime
  • gmstrftime
  • idate
  • localtime
  • microtime
  • mktime
  • strftime
  • strptime
  • strtotime
  • time

Last update on August 19 2022 21:51:15 (UTC/GMT +8 hours)

Description

The date() function displays a formatted local time/date.

Version:

(PHP 4 and above)

Syntax:

date(format, timestamp)

Parameters:

NameDescriptionRequired /
Optional
OptionalType
format Day :
d - The numeric day of the month with
leading zeros (01 to 31)
D - Short day abbreviation (three letters). Mon through Sun.
j - Day of the month without leading zeros ( 1 to 31)
l (lowercase 'L') - Full day name (Sunday through Saturday)
N - ISO-8601 numeric representation of a day of a week
(1 (for Monday) through 7 (for Sunday)
S - English ordinal suffix for the day of the month, 2 characters
(st, nd, rd or th. Works well with j)
w - The numeric day of the week. (0 (for Sunday) through 6 (for Saturday)
z - The numeric day of the year (0 to 365)

Week:

W - ISO-8601 numeric representation of week number of year.
Week starting from Monday
Month :
F - Full month name. (January through December)
m - Numeric representation of a month with leading zeros (01 to 12)
M - Short month abbreviation (three letters). Jan through Dec
n - Numeric representation of a month, without leading zeros (1 through 12)
t -Number of days of a specified month (28 through 31)

Year :
L - Whether it's a leap year (set 1 if leap year otherwise 0)
o - ISO-8601 year number
Y - Numeric year value in 4 digits (1999)
y - Numeric year value in two digits (1999 as 99)

Time :


a - Lowercase am or pm.
A - Uppercase AM or PM.
B - Swatch Internet time (000 through 999)
g - 12-hour format of an hour without leading zeros (1 to 12)
G - 24-hour format of an hour without leading zeros (0 to 23)
h - 12-hour format of an hour with leading zeros (01 to 12)
H - 24-hour format of an hour with leading zeros(00 to 23)
i - Minutes with leading zeros (00 to 59)
s - Seconds, with leading zeros (00 to 59)
u - Microseconds(numeric value) Example : 574925

Timezone :
e - The timezone identifier (Examples: UTC, Atlantic/Azores)
I - Whether the date is in daylights savings time (set 1 for Daylight Savings Time, 0 otherwise)
O - Difference to Greenwich time (GMT) in hours (Example: +0300).
p - Difference to Greenwich time (GMT) with a colon between hours and minutes (Example: +03:00).
T - Timezone abbreviation. (Examples: EST, MDT)
Z - Timezone offset in seconds. The offset for timezones west of UTC is always
negative, and for those east of UTC is always positive. (-43200 through 50400).

Full Date/Time :
c - ISO 8601 date (2004-02-12T15:19:21+00:00)
r - RFC 2822 formatted date. (Thu, 22 Jan 2005 16:01:07 +0200)
U - Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

Optional String
timestamp An integer indicates the unixtimestamp. If a timestamp is not supplied, it gives the current local time. Optional integer

Return value:

The absolute value of the number.

Value Type: Date

Example:

<?php
//Set the default timezone to UTC.
date_default_timezone_set('UTC');
echo "<strong>Display current date dd/mm/yyyy format </strong>"."<br />";
echo date("d/m/Y")."<br />";
echo "<strong>Display current date mm/dd/yyyy format</strong> "."<br />";
echo date("m/d/Y")."<br />";
echo "<strong>Display current date mm-dd-yyyy format </strong>"."<br />";
echo date("m-d-Y")."<br />";
echo "<strong>Display like Monday 6th of March 1996 </strong>"."<br />";
echo date("l jS \of F Y")."<br />";
echo "<strong>Display the above format with time </strong>"."<br />";
echo date('l jS \of F Y h:i:s A')."<br />";
echo "<strong>Display something like: 2010-11-01T00:00:00+00:00 </strong>"."<br />";
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
?> 

View the example in the browser

See also

PHP Function Reference

Previous: date_sunset
Next: getdate

PHP: Tips of the Day

PHP: Showing all errors and warnings

Display errors could be turned off in the php.ini or your Apache configuration file.

You can turn it on in the script:

error_reporting(E_ALL);
ini_set('display_errors', 1);

You should see the same messages in the PHP error log.

Ref : https://bit.ly/34di0ji


  • Exercises: Weekly Top 16 Most Popular Topics
  • SQL Exercises, Practice, Solution - JOINS
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • JavaScript basic - Exercises, Practice, Solution
  • Java Array: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : Conditional Statement
  • HR Database - SORT FILTER: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : String
  • Python Data Types: Dictionary - Exercises, Practice, Solution
  • Python Programming Puzzles - Exercises, Practice, Solution
  • C++ Array: Exercises, Practice, Solution
  • JavaScript conditional statements and loops - Exercises, Practice, Solution
  • C# Sharp Basic Algorithm: Exercises, Practice, Solution
  • Python Lambda - Exercises, Practice, Solution
  • Python Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation


How can I get current date in PHP?

Answer: Use the PHP date() Function 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.

How can I get current date in YMD in PHP?

date("Y/m/d") . "<br>"; echo "Today is " . date("Y.m.d") .

What does NOW () return in PHP?

MySQL function NOW() returns the current timestamp.

What is the YYYY

Date/Time Formats.