dates

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

dates

Post by gurjit »

hi all,

i have a random entry of the way people have entered the month of a date, for example

21 february

has been entered as:
21/feb
21/FEB
21/Feb
21/february
21/February
21/FEBRUARY
21/2
21/02

I tried to run this script with
21/feb and it gave the output:

"2004-12-21" why?
this is my script


Code: Select all

<?php
$dd = '21/feb';
$pieces = explode("/", $dd);

$d = $pieces[0];
$m = $pieces[1];
$y = date("Y");


$term_started = date('Y-m-d',mktime(0,0,0,$m,$d,$y));



echo $term_started;
?>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I doubt mktime() would like "feb" or "february" as an argument. Integers only. Try looking at strtotime() instead.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

tried this and it gave me "2026-07-29"

Code: Select all

<?php

$dd = '21/feb';
$pieces = explode("/", $dd);

$d = $pieces[0];
$m = $pieces[1];
$y = date("Y");

$ths = "$y-$m-$d";
$term_started2 = date("Y-m-d",strtotime($ths));

echo $term_started2;
?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

The manual says that strtotime will accept the following formats: http://www.gnu.org/software/tar/manual/ ... tar_7.html


Thus the following code should work:

Code: Select all

$dd = '21/feb';
$pieces = explode("/", $dd);
 
$d = $pieces[0];
$m = $pieces[1];
$y = date("Y");
 
$ths = "$d $m $y";
$term_started2 = date("Y-m-d",strtotime($ths));
Post Reply