By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,643 Members | 1,797 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,643 IT Pros & Developers. It's quick & easy.

managing date

P: n/a
Hi,

I've got a string that contains a date with the following format :
yyyy-mm-dd hh:mm:ss
ex :
2003-10-10 11:37:47

I'd like to get 4 variables :
- 1 for the year number,
- 1 for the month,
- 1 for the day,
- 1 for the time.

How is it possible ?
Must I use the explode function or is there another way ?

many thanks in advance,
cheers,
--
nyso
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"nyso" <ny**@no-spam.org> wrote in message
news:3f***********************@news.free.fr...
Hi,

I've got a string that contains a date with the following format :
yyyy-mm-dd hh:mm:ss
ex :
2003-10-10 11:37:47

I'd like to get 4 variables :
- 1 for the year number,
- 1 for the month,
- 1 for the day,
- 1 for the time.

How is it possible ?
Must I use the explode function or is there another way ?

I did something like this (but not with the time) and posted an example
recently. I am new to this, but you could explode on the space and you would
have date and time, then explode on the date and get the year, month and
day.

Bill
Jul 17 '05 #2

P: n/a
> I did something like this (but not with the time) and posted an example
recently. I am new to this, but you could explode on the space and you would have date and time, then explode on the date and get the year, month and
day.

Bill


That's what I'm doing at the moment... but it doesn't seem "clean" to me.
I'm searching for a better solution.

--
nyso
Jul 17 '05 #3

P: n/a
Op Thu, 30 Oct 2003 09:43:15 +0100 schreef "nyso" <ny**@no-spam.org>:
I've got a string that contains a date with the following format :
yyyy-mm-dd hh:mm:ss
ex :
2003-10-10 11:37:47

I'd like to get 4 variables :
- 1 for the year number,
- 1 for the month,
- 1 for the day,
- 1 for the time.

How is it possible ?
Must I use the explode function or is there another way ?


Try this:

$date_time="2003-10-15 11:37:47";

$year=strftime("%Y",strtotime($date_time));
$month=strftime("%m",strtotime($date_time));
$day=strftime("%d",strtotime($date_time));
$time=strftime("%H:%M:%S",strtotime($date_time));
--
http://www.techtronix.be
Jul 17 '05 #4

P: n/a
*** nyso wrote/escribió (Thu, 30 Oct 2003 09:43:15 +0100):
2003-10-10 11:37:47

I'd like to get 4 variables :
- 1 for the year number,
- 1 for the month,
- 1 for the day,
- 1 for the time.


<?php
if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
echo "$regs[3].$regs[2].$regs[1]";
} else {
echo "Invalid date format: $date";
}
?>

I hope this helps. It's taken from ereg() reference.
--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #5

P: n/a
"nyso" <ny**@no-spam.org> wrote in message news:<3f***********************@news.free.fr>...
Hi,

I've got a string that contains a date with the following format :
yyyy-mm-dd hh:mm:ss
ex :
2003-10-10 11:37:47

I'd like to get 4 variables :
- 1 for the year number,
- 1 for the month,
- 1 for the day,
- 1 for the time.

How is it possible ?
Must I use the explode function or is there another way ?


You may use:

$date_str = "2003-10-10 11:37:47";
list($y, $m, $d, $hh, $mm, $ss) = sscanf($date_str,"%4d-%2d-%2d
%2d:%2d:%2d");
echo $y;
echo $m;
.....

---
"He who created the god was a fool; he who spreads his name is a
scoundrel and he who worships him is a barbarian."---Periyar, Famous
Tamil Rationalist
Email: rrjanbiah-at-Y!com
Jul 17 '05 #6

P: n/a
$datearr = split("[- ]",$bigfield);

$datearr[0] will contain the year
$datearr[1] will contain the month
$datearr[2] will contain the day
$datearr[3] will contain the time

"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> wrote in message
news:ab**************************@posting.google.c om...
"nyso" <ny**@no-spam.org> wrote in message

news:<3f***********************@news.free.fr>...
Hi,

I've got a string that contains a date with the following format :
yyyy-mm-dd hh:mm:ss
ex :
2003-10-10 11:37:47

I'd like to get 4 variables :
- 1 for the year number,
- 1 for the month,
- 1 for the day,
- 1 for the time.

How is it possible ?
Must I use the explode function or is there another way ?


You may use:

$date_str = "2003-10-10 11:37:47";
list($y, $m, $d, $hh, $mm, $ss) = sscanf($date_str,"%4d-%2d-%2d
%2d:%2d:%2d");
echo $y;
echo $m;
.....

---
"He who created the god was a fool; he who spreads his name is a
scoundrel and he who worships him is a barbarian."---Periyar, Famous
Tamil Rationalist
Email: rrjanbiah-at-Y!com

Jul 17 '05 #7

P: n/a
"Pat Scott" <js********@comcast.net> a écrit dans le message de news:
EK********************@comcast.com...
$datearr = split("[- ]",$bigfield);

$datearr[0] will contain the year
$datearr[1] will contain the month
$datearr[2] will contain the day
$datearr[3] will contain the time

"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> wrote in message
news:ab**************************@posting.google.c om...


thank you very much for your help.
this was very useful for me.

thanks again,
--
nyso
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.