php convert timestamp to specific format - php

I have one field registered_date which is timestamp type in MYSQL. Now I an new with php I need your help.
I am getting this result: 2014-06-19 15:59:49
and I want this output: 19 June 2014 15:59 PM
Note: I am storing timestamp in php variable from sql query.
Any Idea?

http://php.net/manual/en/function.strtotime.php
http://php.net/manual/pl/function.date.php
$date = date('D M Y H:i A', strtotime($timestamp));

Since you tagged the question with mysql here is the mysql solution, you can use date_format() function while selecting the data and you will have desired format.
mysql> select date_format('2014-06-19 15:59:49','%d %M %Y %H:%i %p') as date ;
+-----------------------+
| date |
+-----------------------+
| 19 June 2014 15:59 PM |
+-----------------------+
1 row in set (0.00 sec)

Related

MySQL - saving date and time as DATETIME vs as saving date and time as String

I need to store a deadline, that consists of a date and a time (e.g 2016-05-02 19:02). I am currently using a field that has DATETIME as datatype but the problem is that its automatically saving it as 2016-05-02 19:02:00.
as a solution i was thinking to save the date in a String field.
So i am wondering if i should do that ? any performance advantages/disadvantages ?
Don't fight the database. Use the builtin types unless you really need something that they can't offer(I'd say it's unlikely, though). (And by this I mean that you should use TIME, DATE or similar for times and dates. Then you can do calculations without having to convert values, etc)
If you don't need the seconds then just keep them 00 all the time.
Whether you use DATETIME, DATE & TIME or perhaps TIMESTAMP is up to you, how you use the data. Choose the alternative that makes most sense in your current situation.
As mentioned in the other answer, you should always use built-in data types whenever possible.
In your case, stick with DATETIME and then convert it to whatever format you need in the query using the DATE_FORMAT function, like so:
mysql> SELECT * FROM `mytable`;
+----+---------------------+
| id | mydatetime |
+----+---------------------+
| 1 | 2016-06-06 14:12:00 |
+----+---------------------+
1 row in set (0.00 sec)
mysql> SELECT DATE_FORMAT(`mydatetime`,'%b %d %Y %h:%i %p') AS `mydatetime` FROM `mytable`;
+----------------------+
| mydatetime |
+----------------------+
| Jun 06 2016 02:12 PM |
+----------------------+
1 row in set (0.00 sec)
Reference:
http://www.w3schools.com/sql/func_date_format.asp

Best practice to manage date and days

I am working on a project where I have many operations to manage.
Each operation have an end date and is composed by a certain amount of tasks.
I want to display reminders (a text displayed on the screen) if a task is not done before [end date] - X days.
All the data is stored in MySQL database and I work with PHP and HTML5.
Which datatype is (are) the best to work with date and days (to
perform calculations)?
Can I work with Date() and subtract days in a easy way?
I do not have a specific technical question, but I think sharing best way to proceed is a good thing, right?
I'm curious to know what are the best ways to proceed and open to any proposal!
I recommend to store your date in mysql at field timestamp because you can use default value CURRENT_TIMESTAMP - it very helpful,
and i think you shouldn't worry about it, there is a plenty of functions::
mysql:
select now();
+---------------------+
| now() |
+---------------------+
| 2015-04-08 12:13:18 |
+---------------------+
select now() - interval 1 day;
+------------------------+
| now() - interval 1 day |
+------------------------+
| 2015-04-07 12:13:29 |
+------------------------+
select now() - interval 7 day;
+------------------------+
| now() - interval 7 day |
+------------------------+
| 2015-04-01 12:13:38 |
+------------------------+
select now() - interval 1 month;
+--------------------------+
| now() - interval 1 month |
+--------------------------+
| 2015-03-08 12:13:58 |
+--------------------------+
php:
<?php
var_export([
date('Y-m-d H:i:s', strtotime('now')),
date('Y-m-d H:i:s', strtotime('- 1 day')),
date('Y-m-d H:i:s', strtotime('- 7 day')),
date('Y-m-d H:i:s', strtotime('- 1 month')),
]);
/*
Result:
array (
0 => '2015-04-08 15:15:42',
1 => '2015-04-07 15:15:42',
2 => '2015-04-01 15:15:42',
3 => '2015-03-08 15:15:42',
)
*/
And sometimes very helpful to create table like:
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
in result your field ts will be automatically seted and updated...
You can store dates as DATETIME in your database.
Then in PHP convert it to manageable data using strtotime() and the date() functions
The best data type to work with is the DateTime class.
In order to perform substractions using the DateTime class, you'll need to use the DateInterval class.
You'll need some time to get on your ease using those two classes but it will make formatting or date operations easier afterwoods.

Find out which months have data logged in MYSQL database?

In my database I have the date of each customer order stored in the format 02 Mar 2015
I have data from March and April and I want a query that will return just those 2 months. Once I have data for May it will return the three months etc.
The SQL syntax which I am trying to use is:
SELECT DISTINCT MONTH(DATE_FORMAT(date,'%d %b %y')) FROM orders
However this returns 0 rows. I presume this is an issue with date format.
EDIT:
Sample data from table:
id | date | time | order_id | item | quantity
1 | 02 Mar 2015 | 14:22 | 1029 | clasico | 9
1 | 05 Apr 2015 | 13:58 | 1029 | hindu | 10
try
SELECT DISTINCT MONTH(`order_date`) FROM `orders`
where order_date is the date field in orders
Note: The answer assumes the date column is a varchar rather than a datetime datatype.
The DATE_FORMAT() function is used to display date/time data in different formats. Note: This assumes the data is a datetime data type.
The STR_TO_DATE() returns a datetime value by taking a string and a specific format string as arguments. See the code below in action in the SQL Fiddle demo.
select DISTINCT MONTH(STR_TO_DATE(date, '%d %b %y')) from orders

Convert string date to MySQL datetime [duplicate]

This question already has answers here:
Convert Javascript time to MySQL format using PHP
(4 answers)
Closed 8 years ago.
I have a series of dates stored in my database from an import. They're in the format of
Tue 2 Sep 2014
What would be the best way to convert this to MySQL date format, for example, yyyy-mm-dd hh:mm:ss either using PHP or MySQL.
Thanks
It's easy, just use date() combined with strtotime()
$date = "Tue 2 Sep 2014";
$conv = date("Y-m-d H:i:s", strtotime($date));
echo $conv;
output:
2014-09-02 00:00:00
To omit the time just remove H:i:s.
You can use str_to_date function to convert a string to a date as
mysql> select str_to_date('Tue 2 Sep 2014','%a %e %b %Y') as date ;
+------------+
| date |
+------------+
| 2014-09-02 |
+------------+
You can also format the date as
mysql> select date_format(str_to_date('Tue 2 Sep 2014','%a %e %b %Y'),'%Y-%m-%d %H:%i:%s') as date ;
+---------------------+
| date |
+---------------------+
| 2014-09-02 00:00:00 |
+---------------------+
Check more about date_format and other date functions here
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

Converting string to datetime

I'm trying to enter a date in a mysql table
`ssdate` datetime
The function that produces the date can output it in 3 formats:
1276142400000
Thu Jun 10 00:00:00 GMT-0400 2010
Fri Jun 4 2010
I'm wondering which of these would be easiest to convert to this field format?
Since I'm trying to save only the date, is there a better option than datetime that would work with one of these output formats?
You can use the third format:
date('Y-m-d H:i:s', strtotime('Fri Jun 4 2010'));
Just put the result in your datetime field. If you're going to use a date field instead you can do
date('Y-m-d', strtotime('Fri Jun 4 2010'));
The easiest way would probably be FROM_UNIXTIME(), but 1276142400000 does not appear to be a Unix timestamp:
mysql> SELECT FROM_UNIXTIME(1276142400000);
+------------------------------+
| FROM_UNIXTIME(1276142400000) |
+------------------------------+
| NULL |
+------------------------------+
1 row in set (0.00 sec)
Perhaps it's a Unix timestamp mutiplied by 1000:
mysql> SELECT FROM_UNIXTIME(1276142400000/1000);
+-----------------------------------+
| FROM_UNIXTIME(1276142400000/1000) |
+-----------------------------------+
| 2010-06-10 06:00:00 |
+-----------------------------------+
1 row in set (0.05 sec)
And, if unsure, you always have STR_TO_DATE():
mysql> SELECT STR_TO_DATE('Fri Jun 4 2010', '%a %b %e %Y');
+----------------------------------------------+
| STR_TO_DATE('Fri Jun 4 2010', '%a %b %e %Y') |
+----------------------------------------------+
| 2010-06-04 |
+----------------------------------------------+
1 row in set (0.00 sec)
You can set your column as a varchar(14), will work perfectly with your first format output.
In database I would store time values in in DATETIME field, mainly because of built-in methods for date manipulations (INTERVAL, etc.). One possible alternative is to store UNIX timestamp as numeric value, but I wouldn't recommend that. If in doubt, choose standard solutions.
As for date format to convert from, I would definitely go with UNIX timestamp (#1 option in your case, multiplied by 1000 I guess) as most universal one. All other formats are locale- and timezone-dependent, which is a possible source for a lots of headaches in the future.

Categories