Dispay date formate in Apr 02 in php and mysql - php

$sql="SELECT *
FROM events
WHERE sdate > CURDATE()
ORDER BY STR_TO_DATE (sdate,'%d %F %Y')
LIMIT 1";

You will be glad to code using timestamps for this, let me give you an example:
SELECT UNIX_TIMESTAMP(CURDATE()) will return something like 1586063538.
That is a timestamp, which is easy to work with.
Then in your case, you will have to do a 2 step code
First when retrieving from the database you must use:
SELECT field1, field2, ..., fieldN, UNIX_TIMESTAMP(sdate) as epoch
FROM events
WHERE sdate > CURDATE()
ORDER BY STR_TO_DATE (sdate,'%d %F %Y')
LIMIT 1;
where field1, field2, ... are the other fields you want and epoch will contain the epoch for that date.
Remember naming the fields is always a good practice when coding.
Then using PHP date() function you can parse an epoch timestamp as Apr 02 using:
$timestamp = 1586063538;
$day_formatted = date("M d",$timestamp);
echo($day_formatted); //will be Apr 05 in this case
More information for the date() function in the php documentation here: https://www.php.net/manual/en/function.date.php
If you want to dig deeper on epoch unix timestamps you can look here:
https://en.wikipedia.org/wiki/Unix_time

Related

How to show a day and date in timestamp data (PHP MYSQL)

i have a field with timestamp datatype in my database, this is the format when i added some record
example: this is chapter field of chapter table in database book
id chapter title timepost
1 1 ..... 2013-10-30 23:33:14
i want that format change in my web site like, sunday,10 october 2013 - 23:33
how to do that? thanks
*UPDATE:*Thank you for all your answer guys, all of your answer have worked correctly
Use the mysql DATE_FORMAT function in your SELECT statement:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
For your request you would need:
DATE_FORMAT(timepost,'%W,%e %M %Y - %H:%i')
Almost every language also has a date formating function, php use Date (http://php.net/manual/en/function.date.php) but I believe this must be supplied a int.
You can do like this;
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W, %d %M %Y - %H:%i');
Out put:
'Sunday, 10 October 2009 - 22:23'
For more information you can see :http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
Use PHP date() function:
http://php.net/manual/en/function.date.php
I strongly recommend doing it on the client side. This way you can easily apply custom date and time formatting based in the users locale.
Im my opinion, formatting should not be done on the SQL server side. Moving string formatting from the SQL server to the web server takes the load from the SQL server and gives it more power to run queries.
Use DATE_FORMAT function
select date_format(timepost,'%W, %M %e, %Y %h:%i %p') from table;
date('l,j F Y - H:i', $timepost);
*You could substitite the format characters
Use PHP's date() function:
$date = date('l, d F Y - H:i', $date_from_mysql);
Use templates from PHP's Manual: http://php.net/manual/ru/function.date.php

php convert time() to date format on the fly for sql

I have a while statement to echo some data to the user from specific dates.
I have stored the dates in my data base in this format: time() I want to write my sql statement in this format to check only the date date("m/d/y", some time). like so:
$page_visits_count = $mysqli->query("SELECT COUNT(`id`) FROM `visits` WHERE date('m/d/y', `time`) = '$date' ");
This part date('m/d/y', ``time``) is obviously wrong, but how would I be able to fix this problem ?
Note: I tried adding one day (3600 * 24) to my time. But that sometimes put me the middle of the next day.
If I understand correctly you're wanting to convert a UNIX timestamp (stored in DB column time) into a date format, then compare that date to the $date variable.
If that's true, FROM_UNIXTIME is what you're probalby after:
"SELECT COUNT(`id`) FROM `visits` WHERE FROM_UNIXTIME(`time`, '%m/%d/%Y') = '$date'");
You'll probably want to tweak the %m/%d/%Y part to match the format of your $date variable, see the FROM_UNIXTIME docs I linked to for a list on what formats are available.

Format mysql timestamp to something meaningful

My database uses a TIMESTAMP column for each article which gets data written to it whenever an article is written to the database. This is done automatically by the database using CURRENT_TIMESTAMP as the default value for the column.
Unfortunately, the date appears as 2011-11-24 19:26:57 which is not ideal for - well - anything.
What I'd like to do is write it to my page in the same format as Thu 24 Nov 2011 19:26:57.
Any advice?
which is not ideal for - well - anything.
You are wrong. It is indeed and exclusively ideal for the sorting dates.
And for the formatting both PHP and mysql has strtotime()+date() and DATE_FORMAT() respectively.
Also note that you may wish to change TIMESTMP to DATETIME format, as the former can be easily altered by accident and thus spoil whole database.
you can use MySQL DATE_FORMAT function which formates the timestamp in a way you like see this link http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
You could use MySql builtin function to format date-time column
SELECT DATE_FORMAT(your_col, '%a %d %b %Y %k:%i:%s')
FROM your_table
take a look at : http://php.net/manual/en/function.date-parse.php This builds an array of a string representing a date, you can then format "your" date however you would like to.
There's also the mysql function DATE_FORMAT
e.g. SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y');
-> 'Sunday October 2009'
You can do this in either PHP or MySQL:
PHP code:
<php
$formattedDate = strftime('%a %e %b %Y %T',strtotime($mysqlDate));
?>
MySQL code:
SELECT DATE_FORMAT('%W %e %M &Y %T', date_col);
Info: http://www.php.net/manual/en/function.strftime.php , http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
You actually have a couple of options. One of them is to use MySQL directly.
An example to match you need would be:
SELECT field1, field2, DATE_FORMAT(dateField, '%a %e %b %Y %T') AS theDateFROM table;
Another option is to use the date() and strtotime() functions from PHP with the timestamp directly:
<?php echo date("D j M Y H:i:s", strtotime($datetime_from_database)); ?>
I hope this is good for you
1
<?php date("D M j G:i:s T Y"); ?>
2
<?php date("F j, Y, g:i a"); ?>
1
Sat Sep 9 4:13:18 CDT 2006
2
September 9, 2006, 4:13 am

date time in php stored

I have stored date time in mysql from a tutorial as:
2011-02-10 01:21:39
From this, how can I extract the individual elements using php?
Say I just want the year?
or even just the complete date if thats not possible?
See the strtotime() function. This will convert your date string into a timestamp. Then use date() to pull out the parts you need:
$time = strtotime('a string containing some description of the time and date');
$year = date('Y', $time);
Why not select the elements from MySQL directly?
select year(datefield), month(datefield), day(datefield) from yourtable
would return
+---------------+
| 2011 | 2 | 10 |
+---------------+
of course, if you want to do datemath in PHP, it'd be better to select the date as a UNIX_TIMESTAMP from mysql, which returns time in seconds from Jan 1/1970, which you can feed directly into PHP's date system;
SELECT UNIX_TIMESTAMP(datefield) ...
$timestamp = mysql_fetch(...)
$date = date($timestamp);
You can use the extract function of mysql from within your query like this:
SELECT EXTRACT(YEAR FROM yourDatefield) AS year,
EXTRACT(MONTH FROM yourDatefield) AS month,
EXTRACT(DAY FROM yourDatefield) AS day,
EXTRACT(HOUR FROM yourDatefield) AS hour,
EXTRACT(MINUTE FROM yourDatefield) AS minute,
EXTRACT(SECOND FROM yourDatefield) AS second
FROM
yourTable
Check out the extract function for more info.
You mean date('Y', strtotime('2011-02-10 01:21:39'));?
Go with #Marc's Solution if you are looping through MySQL result set
Use php's date(). You can format how ever you need.
date()
<?php $original = $row['time'];
$date = date_create($original);
echo date_format($date, 'Y');?>
just replace the $original with your own variable.
definitely check the php date() http://php.net/manual/en/function.date.php
Take a look at the Date and Time Functions for MySQL
For example, you can:
SELECT YEAR(DateColumn) FROM YourTable
date($format, strtotime($yourStringFromDB)) see PHP docs for format strings.
SELECT * FROM Dates
WHERE Date LIKE '%02%'

SELECT 2010-10-24 09:02:46 post as 'Oct 24, 2010 at 9:02AM'

How can I convert my date field from mysql from 2010-10-24 09:02:46 to post on my site as 'Oct 24, 2010 at 9:02AM'
(SELECT TIME_FORMAT(`dPostTime`, '%b %e %l:%i %p')) as post_time
This won't post the date, just the time.
Thanks so much!
SELECT DATE_FORMAT(dPostTime, '%b %e, %l:%i%p') AS post_time
To replace %b %e with "Today" :
SELECT
CASE WHEN DAY(dPostTime) = DAY(NOW()) THEN
DATE_FORMAT(dPostTime, 'Today at %l:%i%p')
ELSE
DATE_FORMAT(dPostTime, '%b %e, %l:%i%p') END AS post_time
The description of the TIME_FORMAT function says:
This is used like the DATE_FORMAT() function, but the format string may contain format specifiers only for hours, minutes, seconds, and microseconds.
So use the DATE_FORMAT function if you want to format the date as well.
I'd suggest the better option is to select it from the database as a timestamp value, and then use PHP's date() function to output the text value to the site. Timestamps are much easier to work with than formatted dates. Even if all you want to do is output it straight to the web page, I would still prefer to leave the formatting to PHP rather than SQL.
SQL:
SELECT UNIX_TIMESTAMP(dPostTime) as post_time ...
PHP:
print date('M d Y h:iA', $data['post_time']);

Categories