I have dates stored in a mysql table, they are set to store as CURRENT TIMESTAMP in each row and are stored as follows:
2010-05-29 01:17:35
but what i am trying to do is somehow use PHP to be able to seperate everything and create a date more like:
May 29 2010 1:17 AM
can anyone at least direct me in the right path that i should take. any help is greatly appreciated!
echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35'));
http://php.net/manual/en/function.date.php
You have two solutions :
Use strtotime() to parse the date to a timestamp, and date() to re-format it to a string
Or use the DateTime class
In PHP code, this would mean using :
echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35'));
Or :
$dt = new DateTime('2010-05-29 01:17:35');
echo $dt->format('M j Y g:i A');
strtotime + date is the solution you'll see used the most ; but it is not the best solution : with those, you'll work on UNIX Timestamps, which means a limited range of dates (from 1970 to 2038, if using 32 bits integers).
ON the other hand, using the DateTime class, there will be no limit to the range of dates you can work with.
If you have a DATETIME field in your table and you only want the date field, then:
SELECT DATE_FORMAT(timestamp,'%M %D, %Y') FROM Mytable;
or:
SELECT DATE_FORMAT(timestamp,'%Y-%m-%d') FROM Mytable;
where timestamp is your column name.
Related
I have to migrate old data to a new website and in the mysql-table is a column called date with a example value of "1308355200" but which datenformat is that can you help me?
Explainations
A timestamp is a way to communicate a date easily. You are aware that in some region, date format are not the same. And that's a problem in programing because we would have to manage every format, in every programing language. Timestamp is universal and most of the programing language (if not all of them) are able to manage timestamp.
Now, PHP offers you a way to translate that timestamp into something more readable with the date() function.
Source-code
<?php
/* database processing here... */
$date = 1308355200; /* value you got from a database query */
$datestring = date('d F, Y', $date);
/* d : day, F : full month, Y : year */
echo 'The date we got from the database: ' . $datestring;
Output
The date we got from the database: 17 June, 2011
Documentation
Wikipedia : timestamp.
PHP : date().
Online demo
Online demo.
This is a Timestamp
Convert to date:
$timestamp = "1308355200"; //This is a Timestamp.
echo date('d-m-Y',$timestamp); //Convert to date.
Result:
2011-06-18
Refer Date format: http://php.net/manual/en/function.date.php
I've been playing around with the Bing API using json and PHP. The array spits out the following for dates:
[end] => /Date(1354867200000)/
[lastModified] => /Date(1349441488000)/
I thought this was a unix timestamp, but it I don't think it is. What I did was a preg_replace like this
$last_updated = $resource->lastModified;
$last_updated_timestamp = preg_replace('/[^0-9.]*/','',$last_updated);
Then tried to convert it to a date
$last_updated_date = date('l F d Y g:i:s A',$last_updated_timestamp);
The results that it's showing me for date range back from the year 1967 to 2000. Is this a different kind of timestamp that I don't know of? If so, how do I correct this? Any help would be appreciated!
The number part is milliseconds-since-the-Epoch (January 1, 1970 at midnight — the milliseconds version of a unix timestamp). This is a fairly conventional way to represent dates in JSON (since JSON doesn't have a date type).
So getdate(theNumber / 1000) will give you the date (since getdate expects seconds, not milliseconds, since The Epoch).
If what you want to do is convert a unix timestamp to date format, you can do it by doing the following:
date("F j, Y g:i a", strtotime($unix_timestamp));
Where $unix_timestamp is your unix timestamp in this case.
You can always print it out for testing purposes by adding echo before it.
So in this case it could be:
$last_updated_date = date("F j, Y g:i a", strtotime($last_updated_timestamp));
I am storing the date of entry in mysql via TIMESTAMP and default value UNIX_TIMESTAMP, however when I bring it back it's 2011-08-16 11:43:52 and if I try to style it with
<?= date('F j, o', $a['time']) ?>
It just does the timestamp from zero, bringing back December 31, 1970
Why? And how can I fix it?
Use
SELECT UNIX_TIMESTAMP(field)
When UNIX_TIMESTAMP() is used on a TIMESTAMP column, the function returns the internal timestamp value directly, with no implicit “string-to-Unix-timestamp” conversion.
Manual
A MySQL TIMSTAMP column is stored as YYYY-MM-DD HH-MM-SS, even if you input a UNIX_TIMESTAMP it will still be stored in that format.
So what you can do is:
<?= date('F j, o', strtotime($a['time'])) ?>
Demo: http://codepad.org/jBLR2KpH
date() takes a number and not a string. MySQL always returns your date as a string regardless of its internal representation. Try this:
<?php echo date("F j, o", strtotime($a['time']); ?>
Not sure whats up with the code
$date = strtotime("%b %d, %Y", $datedata);
$time = strtotime("%I:%M:%S %p", $datedata);
The time i gets from the DB is 1298747601 and is the $datedata
I have date_default_timezone_set('America/Los_Angeles'); at the top of the script.
Use strftime() or date(), not strtotime(). Strtotime is meant to format textual dates, not timestamps.
It's better to use the DATETIME datatype in your database instead of an int containing a timestamp. Using a DATETIME means easier calculating with dates and you can format the date directly in your query.
strtotime() is for converting a string to a time like, $timestamp = strtotime("1:33 PM EST Next Friday");.
You want date(), which takes a format string and a timestamp to create a formatted date time string, and uses the current time (from time()) if no timestamp is passed, like date("h:i:s A T, M jS, Y", $timestamp) which would output something like "1:33:00 PM EST, March 4th, 2011". Also, note that if you want to do words in the format, like "23rd of January", and the letters in the words are also date formatting characters, in this case the 'o' in 'of' is the ISO-8601 year number formatting option, you must escape it with a \ slash like "jS \of F". As the f is not a formatting option, it does not need to be escaped, but if it did, then it would be "\o\f"
There's also a DateTime object that's built into more recent PHP versions that you can look into.
I disagree with Ray that it is better to store a DateTime datatype in SQL rather than an integer timestamp. They're about the same. You can still search for date ranges in the DB simply by finding numbers greater than X timestamp and less than Y timestamp, and, as a plus, everything that you get from the DB is already in a timestamp format that can be used easily by PHP without having to convert it to a proper timestamp.
right now I'm storing a date in the database, in the format date("Y-m-d") and im storing it in date column.
Now, I've retrieved it from the database but how can i display it like
October 31st 2010
Thanks!
Convert the date to a timestamp using strtotime and format it using date.
echo date('F jS Y', strtotime($databaseDate));
The preferred way going forward should be the use of the DateTime class though:
date_default_timezone_set('Asia/Tokyo');
$date = new DateTime($databaseDate);
echo $date->format('F jS Y');
Use date_format in your SQL query.
Example: date_format(somefield, '%d-%m-%Y, %h:%i %p')