Solved

Echo time from date/time field.

Posted on 2006-11-29
8
236 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use embargo Date/time in php 29 38
Using javascript confirmation for a delete in mysql 5 31
Php recording post 4 43
How do I fix this UPDATE error? 7 23
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

773 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