Solved

Echo time from date/time field.

Posted on 2006-11-29
8
237 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

829 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