todays date in php

They have: 426 posts

Joined: Feb 2005

i want to find todays date (not literally) using php. Im not sure how to do it i used getdate() however it returned a date in 1969 i assume this is the unix start date or something, how do i get to today?

demonhale's picture

He has: 3,278 posts

Joined: May 2005

This

date("date format",[timestamp])

or this

date("F j, Y, g:i a")

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

See http://www.php.net/date for more info.

-Greg

They have: 426 posts

Joined: Feb 2005

mmmm yes this has been done only a few of the dates are ok but some are coming out saying 1969? Does the time stamp have to be in []?

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Post the code you ran to get correct and incorrect dates. The only ways I can think of to get an incorrect date in 1969 is if you supplied an improper timestamp, or if the system's time is itself wrong.

The timestamp doesn't need to be in []. The square bracket notation just means the parameter is optional. In this case, missing the timestamp argument will cause it to default to the value of time().

Smiling

They have: 426 posts

Joined: Feb 2005

I cant seem to insert a timestamp in to mysql? I have used this in my query to insert the timestamp:

Date_created='time()',
'

My database field is set to "timestamp", and default "CURRENT_TIMESTAMP" attributes " ON UPDATE CURRENT_TIMESTAMP" but when i insert into the database it stays at this:

0000-00-00 00:00:00

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

The database will create the timestamp for you. You don't have to supply a value using PHP's time(). Smiling

They have: 426 posts

Joined: Feb 2005

thanks, i deleted my inserts in the script becuase i gave up and now just checked and there all there!!
So i can still order by date even though they are actually in a date format? Mysql and php will know will they?

i can use the date() function to out put them?

He has: 1,380 posts

Joined: Feb 2002

You can do things like

SELECT things FROM wherever ORDER BY date_stamp DESC
'
and it just 'magically' works.

As for outputting, you can just pull the data out of the database, and format it. For example (in PHP):

$sql = mysql_query("SELECT date_stamp FROM whatever WHERE id = '$id'");
$qry = mysql_fetch_array($sql);

$pre_date = $qry['date_stamp'];
$format = 'D of F';

$form_date = date($format, strtotime($pre_date));

echo $form_date;
'

Presto.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.