Solved

Echo time from date/time field.

Posted on 2006-11-29
8
235 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
ID: 18042491
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
ID: 18042497
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
ID: 18042542
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
ID: 18042550
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:alex_code
ID: 18042601
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
ID: 18044711
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
ID: 18045100
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
ID: 18045276
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 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

21 Experts available now in Live!

Get 1:1 Help Now