PHP Date from strtotime with current time - php

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?

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)

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.

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.

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');

Related

adding one month to date using strtotime error with formatting php

Somebody please help me to format the date. I'm not familiar with date formatting.
I want to increment the date by one month but i get this error:
"A non well formed numeric value encountered"
$bseDate=$_POST['lastBseDate']; //this will return 2014-04-03
$nextBse = date('y-m-d', strtotime("+1 month", $bsedate));
Use Datetime() for all date manipulation as it takes Daylight Savings Time and more into consideration and is more readable and maintainable.
$nextBse = (new DateTime($_POST['lastBseDate']))->modify('+1 month')->format('y-m-d');
To answer your question, strtotime gives a timestamp from a string, but since you are providing the optional base time it needs to be a timestamp as well:
$nextBse = date('y-m-d', strtotime("+1 month", strtotime($bseDate)));

PHP: how to create date before the Epoch (1970) using Date instead of DateTime?

In my PHP script I've got a function handling birthdays like so:
$dateTime = \DateTime::createFromFormat('U', $time);
The problem is that this returns false with negative $time numbers (i.e. dates before 1-1-1970). In the PHP docs there's a comment saying that indeed
Note that the U option does not support negative timestamps (before
1970). You have to use date for that.
I'm unsure of how to use Date to get the same result as DateTime::createFromFormat() gives though. Does anybody have a tip on how to do this?
If you just need to format a UNIX timestamp as a readable date, date is simple to use:
// make sure to date_default_timezome_set() the timezone you want to format it in
echo date('Y-m-d H:i:s', -12345);
If you want to create a DateTime instance from a negative UNIX timestamp, you can use this form of the regular constructor:
$datetime = new DateTime('#-12345');

strtotime() in php returning always 01-01-1970

I am using a variable to get the date from the database,then i am passing that variable to strtotime function to get the desired format,but it always returning wrong date.perhaps there is a problem in passing a variable in strtotime function.please suggest me guys,how should i get the correct date in correct format.
Here is what i am trying to do
$date = $fetch_user['date'];
$newDate = date("d-m-Y", strtotime($date));
$day = date('l', strtotime($newDate));
echo $newDate;
echo "-----";
echo $day;
exit;
January 1, 1970 is the so called Unix epoch. It's the date where they started counting the Unix time. If you get this date as a return value, it usually means that the conversion of your date to the Unix timestamp returned a (near-) zero result. So the date conversion doesn't succeed. Most likely because it receives a wrong input.
In other words, your strtotime($date) returns 0, meaning that $date is passed in an unsupported format for the strtotime function.
So you'll have to check for yourself $date, before calling strtotime at all.
01-01-1970 means you probably get 0 as a result of strtotime(). You are probably using a format, that this function cannot understand. The PHP documentation states:
The function expects to be given a string containing an English date
format and will try to parse that format into a Unix timestamp (the
number of seconds since January 1 1970 00:00:00 UTC), relative to the
timestamp given in now, or the current time if now is not supplied.
So it is not really flexible. You might want to try DateTime::createFromFormat instead. Take a look at it's documentation.
Basically you have to specify the format of the date string, that you give as an input as well. That way you can use whatever date format you want.
Example from php.net:
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');

Date / Time showing odd value in PHP

I have wierd issues with time / date in PHP this year. Code have not changed at all and my dates are bugged.
Code is for example:
$date = strtotime($order['date']);
$dateNew = date('Y-m-d h:i A', $date);
print $dateNew;
Returns 1969-12-31 07:00 PM for some reasson, altough:
print $order['date'];
Returns 2013-01-12 18:25:43
I'm confused because I'm quite sure that my code is correct.
I dare you to solve this bugger!
The function strtotime() was made for transform English into date format.
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
As i don't know what is really into your $order variable i will suggest 2 solutions :
Maybe you can avoid the strtotime function and replace it by date() directly like this :
$order = ['date' => '2013-01-12 18:25:43'];
$date = date($order['date']);
It works well here: http://codepad.viper-7.com/cbNA87
Or, if it's not working consider to use mktime(), it will convert the date into seconds since the epoch.
The Unix epoch is the reference point for all time stamps. PHP calculates the times from this date in seconds.
The $date should be null and your server in the east coast of the US so it's returns the epoch :)
PHP returns the date 1969-12-31 when there is not a proper date. So if you did
$date = 0;
$dateNew = date('Y-m-d', strtotime($date));
Your result would be 1969-12-31, since that is the default Unix epoch time. http://php.net/manual/en/function.time.php
Unexpected dates of "1969-12-31 07:00 PM" means something went wrong with date() .
your strototime($order['date']) is probably returning false (failing to parse it to a unix timestamp).
Try this and ensure its returning an int (not false)
var_dump($order['date'], strtotime($order['date']));
See the error state of date: http://php.net/date
See the return values of strtotime: http://php.net/strtotime

php strtotime displaying month as day

I am constructing a diary that has weekly views which I thought I had cracked because the dates seemed to appears as correct. It wasn't until my MySQL queries kept returning what seemed like random results that I realized the the month is actually being seen as the day instead.
$week_number = date("W");
$year = date("Y");
for ($day=0; $day<=6; $day++) {
$daily_date = date('d/m/Y', strtotime($year."W".$week_number.$day))."\n";
$StartDate = date('d', strtotime($daily_date));
}
echo $starteDate;
$startDate returns the number of the month rather than the day and sure enough date('m', strtotime($daily_date)) returns the day rather than the month.
I can't understand where I have made this silly mistake so any help would be appreciated.
This is because of the Americanisation of dates - strtotime will read the date as m/d/Y rather than d/m/Y.
The ISO for dates is Y-m-d and for ease I would use this format when doing any kind of date manipulation.
That code is horrible. You're converting dates to strings multiple times. There is absolutely NO reason to take your $year . w. $week_number.etc... value, convert it to a string, then convert that string back to a date just to extract the day value.
As well, d/m/Y is a horrible format to use for date transport, because... riddle me this, what is 01/02/03. Is that 3rd Feb 2001? 1 Mar 2002? If you can't figure it out, how can you expect strtotime to be better at it? it's fairly smart, but it's not omnicient, and it's DEFINITELY not infallible. A 4digit year does make it a bit easier, but you can still end up with d/m v.s. m/d confusion.
Why not simply
$StartDate = date('d', strtotime($year."W".$week_number.$day));
or better yet, use the DateTime class and select an appropriate DateInterval

Categories