1

I have been looking online for this answer and have come up empty...I am extremely tired so I thought I would give this a go....

I have a variable that has a date from a textbox

$effectiveDate=$_REQUEST['effectiveDate']; 

What I am trying to do is take this date and add the current time

date('Y-m-d H:i:s', strtotime($effectiveDate)) 

When I echo this out I get 1969-12-31 19:00:00

Is this possible? Can someone point me in the right direction?

3
  • 3
    Well, what format is the date in? Commented Feb 10, 2012 at 20:07
  • from the $_REQUEST['effectiveDate'] ? Commented Feb 10, 2012 at 20:07
  • Well, what format is $_REQUEST['effectiveDate'] in? Commented Feb 10, 2012 at 21:21

4 Answers 4

8

I found a solution to my problem....

$currentDate = date("Y-m-d"); $currentTime = date("H:i:s"); $currentDate = date("Y-m-d H:i:s", strtotime($currentDate . $currentTime)); echo $currentDate; 

This takes a date from variable in one format and takes the date from another variable in another format and puts them together :)

Thanks everyone for their time.....

DateTime::createFromFormat 

would also work but only if you have PHP 5.3 or higher...(I think)

Sign up to request clarification or add additional context in comments.

Comments

2

The effectiveDate string is not in a format that strtotime recognizes, so strtotime returns false which is interpreted as 0 which causes the date to be displayed as January 1, 1970 at 00:00:00, minus your time zone offset.

2 Comments

@user1193385: can you post $effectiveDate output ?
that date is from a textbox with a javascript pop-up where the user selects a date in that format as well, I am looking to add the current time
1

The result you see is caused by the entered date not being in a format recognised by strtotime. The most likely case I can think of without knowing the format you used is that you used the US order of putting the month and day the wrong way around - this confuses strtotime, because if it accepts both then it can't distinguish February 3rd and March 2nd, so it has to reject US-formatted dates.

The most reliable format for strtotime is YYYY-MM-DD HH:ii:ss, as it is unambigous.

3 Comments

In fact, it's hard-coded to treat xx/xx/xxxx as US format. Which is a terrible design. As such, you're best off avoiding the use of this function - except maybe to parse dates read from a database. If you do use strtotime to parse user-entered dates, then at least add some validation to make sure the user has used a sensible, unambiguous format.
What you can do is parse the date using strtotime and then convert it back to one or more formats using date. If the entered string doesn't match one of the converted-back strings, then reject it. This would also avoid the weird handling of out-of-range month and day values.
@Stewart Personally I strongly recommend the use of <input type="date" placeholder="YYYY-MM-DD" /> coupled with a nicely-implemented JavaScript fallback for unsupporting browsers. This ensures you'll be dealing with an ISO-standard date format, unless the user's stuck in last millennium with an outdated browser and JavaScript turned off... but in that case a server-side message can tell them what format to use.
0

The date is just a timestamp, it is not object-oriented and i don't like it.

You can use the DateTime object.

The object-oriented best way is:

$effectiveDate=$_REQUEST['effectiveDate']; // here you must pass the original format to pass your original string to a DateTimeObject $dateTimeObject = DateTime::createFromFormat('Y-m-d H:i:s', $effectiveDate); // here you must pass the desired format echo $dateTimeObject->format('Y-m-d H:i:s'); 

3 Comments

I get an error Call to undefined method DateTime::createfromformat()
PHP is case sensitive try DateTime::createFromFormat() instead DateTime::createfromformat()
It's inconsistent. Some kinds of identifiers are case-sensitive and others aren't. No idea why. To avoid confusion it's best not to rely on this, but (a) always refer to an entity by the same capitalisation with which it was defined (b) never define two identifiers that differ only in case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.