Solved

Echo time from date/time field.

Posted on 2006-11-29
8
234 Views
Last Modified: 2013-12-12
Hi.

I have a date/time field in mysql database and i want to echo just the time and then just the month...etc

example :
field "chronos" data : 2006-11-29 16:00:00

echo time(chronos) = 16:00
echo year(chronos) = 2006
echo month(chronos) = 11
echo date(chronos) = 29

Thanks in advance.
0
Comment
Question by:CodedK
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 35

Expert Comment

by:Raynard7
Comment Utility
if you are doing this in mysql you could just do these as part of the query.

ie

select time(chronos) as tc, year(chronos) as yc, month(chronos) as mc, dayofmonth(chronos) as dc from tableName

0
 
LVL 35

Expert Comment

by:Raynard7
Comment Utility
or you could format the date in the form

string date ( string format [, int timestamp] )

using the arguements located at http://au2.php.net/manual/en/function.date.php
0
 
LVL 16

Author Comment

by:CodedK
Comment Utility
I want to do it with php and i dont know how date function can help.
I mean i want to extract for example only the hour from a given string and after only the year...


0
 
LVL 9

Assisted Solution

by:alex_code
alex_code earned 55 total points
Comment Utility
Hi Codedk

first of all you have set a field with datetime property as i understand so the result is 0000-00-00 00:00:00 and you want to take several parts like only time or only date e.t.c.

one way to do this is with sql commands for example:

SELECT DATE_FORMAT('2006-12-31 04:23:00 or you can put the dynamic data from the field datetime', '%W %M %Y');
       this is gone give you 2006-12-31  

SELECT DATE_FORMAT('2006-12-31 04:23:00', '%H:%i:%s');
      this is gone give you '04:23:00'

SELECT DATE('2006-12-31 04:02:03');
this is gone give you 2006-12-31  

hope this to help you!

Regards

Alex_Code
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 9

Expert Comment

by:alex_code
Comment Utility
Oh, Codedk sorry...

you said that you want it in php...

So try this:

echo date("M", mktime()-$chronos);
echo date("Y", mktime()-$chronos);
echo .... e.t.c.

i$chronos  is your variable as you said to your post...
0
 
LVL 8

Expert Comment

by:jk2001
Comment Utility
mktime()-$chronos?

I think you mean strtotime($chronos).

It's probably more efficient to split it up in SQL.  strtotime() is a "smart" algorithm that converts English strings into timestamps.  As you can imagine, it's complex.  With SQL, the server knows the format, so parsing the parts is simpler.
0
 
LVL 14

Expert Comment

by:ygoutham
Comment Utility
you already have it in date/time format in mysql. all you need is to select it with date_format(chronos, '%r %H')

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

see the page and do a search for Date_Format and you see a table to select whatever you want from the field.  use a combo and get your results
0
 
LVL 14

Accepted Solution

by:
ygoutham earned 70 total points
Comment Utility
you can do

select date_format(chronos, '%r,H=%H,h=%h,%Y,%y,%m,%M') from table_name;

this gives a "," separated value for you to work with in php which you can use to explode and get as individual values
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now