Jump to content

Recommended Posts

Hi,

 

Im posting a date in the format : dd/mm/yyyy

 

and am wanting to store it in a database as a string.

 

I'm using the following code:

 

$dayoff=$_POST['dayoff'];

$dayoffst=strtotime('$dayoff');
$date=date('dS M Y',$dayoffst);

 

However when I echo the $dayoffst it doesn't return anything :(

 

any ideas?

Edited by mattichu
Link to comment
https://forums.phpfreaks.com/topic/269457-basic-problem/
Share on other sites

Is $dayoff a proper unix timestamp? Because, that is what the second parameter of date requires. If you are using a formatted date, then you need to use strtotime on it.

 

However, as has been stated, don't store your date in the database this way, as you will lose all functionality of working with dates in the database. You should just cast the date to your desired output via the correct database functions.

Link to comment
https://forums.phpfreaks.com/topic/269457-basic-problem/#findComment-1385166
Share on other sites

strtotime() date format examples

<?php  
$dates = array (
'25/12/2012',
'25/12/12',
'25.12.12',
'25-12-12',
'25-12-2012',
'2012-12-25',
'12-12-25',
'25-12-2012',
'12/25/2012',
'25-DEC-12',
'25 dec 2012'
);

foreach ($dates as $d) {
printf('%s -> %s<br />', $d, date('j F Y', strtotime($d)));
}
?>

output:

25/12/2012 -> 1 January 1970	x
25/12/12 -> 1 January 1970	  x
25.12.12 -> 25 December 2012
25-12-12 -> 12 December 2025	x
25-12-2012 -> 25 December 2012
2012-12-25 -> 25 December 2012
12-12-25 -> 25 December 2012
25-12-2012 -> 25 December 2012
12/25/2012 -> 25 December 2012
25-DEC-12 -> 25 December 2012
25 dec 2012 -> 25 December 2012

Edited by Barand
Link to comment
https://forums.phpfreaks.com/topic/269457-basic-problem/#findComment-1385209
Share on other sites

You should store dates in the proper YYYY-MM-DD format, in the proper field type (DATE) in the database.

I still just store the unix_timestamp (INT) and format it in PHP although I'm getting some legs in some of the date/time functions of MySQL so I might start working out some "proper" ways of storing date/time in MySQL. :happy-04:

Link to comment
https://forums.phpfreaks.com/topic/269457-basic-problem/#findComment-1385264
Share on other sites

Hey, how did I miss the whole first part of the OP.

 

It would accept dd-mm-yyyy or dd.mm.yy. Both of which could be fixed with a simple str_replace.

 

As long as you have 4-digit years otherwise confusion reigns

 

dd mm yy(yy) formatted inputs

03.12.12 -> 15 October 2012	 ???
03.12.2012 -> 3 December 2012
03-12-12 -> 12 December 2003
03-12-2012 -> 3 December 2012

Edited by Barand
Link to comment
https://forums.phpfreaks.com/topic/269457-basic-problem/#findComment-1385276
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.