PHP strtotime() function off using "1-January-2013" - php

I am collecting a user's birthday from a registration form and am hoping to convert this to a timestamp using strtotime before storing it in a database, but this isn't going so well.
I have them select their date in 3 select boxes, one is for the day, another the month, and lastly the year, and then I feed those into strtotime. The problem is, I am inputting "1", "January", and "2013" for the variables previously listed and am feeding them into strtotime like this:
$user->birthday = strtotime($input['bday'].'-'.$input['bmonth'].'-'.$input['byear']);
This reads when echoed as "1-January-2013", yet the timestamp it spits out renders to "Dec 31, 2012" using:
date_default_timezone_set("America/Los_Angeles");
echo date("M d, Y", 1356998400);
I have been testing different methods profusely but cannot get this to work. I'd appreciate any assistance.

Are you setting date_default_timezone_set() before the call to strtotime() or after? Since the default timezone is UTC, and LA is UTC-08.00, that could account for the difference.

You have to call date_default_timezone_set before calling strtotime, so that you have the same timezones when parsing the input date and when you're displaying the date.

Not too sure how your code is failing, but this works for me:
// Set the test data.
$test_data = '1-January-2013';
// Get the Unix datetime from the test data.
$date = strtotime($test_data);
// Format the Unix datetime.
$dateFormat = date("M d, Y", $date);
// Output for debugging.
echo 'test_data: ' . $date . '<br />';
echo 'date: ' . $date . '<br />';
echo 'dateFormat: ' . $dateFormat . '<br />';
Output from my side using PHP 5.5 is:
test_data: 1357016400
date: 1357016400
dateFormat: Jan 01, 2013
But on the outside chance those - dashes are choking things, try this:
// Set the test data.
$test_data = '1-January-2013';
// Filter out dashes from the '$test_data'
$test_data = preg_replace('/-/', ' ', $test_data);
// Get the Unix datetime from the test data.
$date = strtotime($test_data);
// Format the Unix datetime.
$dateFormat = date("M d, Y", $date);
// Output for debugging.
echo 'test_data: ' . $date . '<br />';
echo 'date: ' . $date . '<br />';
echo 'dateFormat: ' . $dateFormat . '<br />';
And again, the output from my side using PHP 5.5 is:
test_data: 1357016400
date: 1357016400
dateFormat: Jan 01, 2013

It's definitely a time zone issue. 1356998400 is 01 / 01 / 13 # 12:00:00am UTC, so when you do this:
date_default_timezone_set("America/Los_Angeles");
echo date("M d, Y", 1356998400);
The Unix time 1356998400 is Dec 31, 2012 in Los Angeles! I suspect you're getting your timestamp in UTC and then outputting it with PST.

Related

Format the infusionsoft return date using php

i worked on the code to get subscription start date & end date from the infusionsoft and it return in this format
Start date=20151217T00:00:00
EndDate=20161217T00:00:00
how i can format to YYYY/MM/DD H:M:S ?
and what the 'T' stands for
The T, according to the the ISO 8601 standard, is the delimiter between the date and time parts.
See: ISO 8601 standard, Wikipedia
There are two functions you could use:
strtotime(date_time_string)
Converts a date/time string into a timestamp.
date(format, timestamp)
Formats a timestamp into a string according to "format".
The example code…
$so_string = '20151217T00:00:00';
$eo_string = '20161217T00:00:00';
$sd_stamp = strtotime($so_string);
$ed_stamp = strtotime($eo_string);
$f_datetime ='Y/m/d H:i:s';
echo 'Start date<br />';
echo ' original: ' . $so_string . '<br />';
echo ' timestamp: ' . $sd_stamp . '<br />';
echo ' formatted: ' . date($f_datetime,$sd_stamp) . '<br /><br />';
echo 'End date<br />';
echo ' original: ' . $eo_string . '<br />';
echo ' timestamp: ' . $ed_stamp. '<br />';
echo ' formatted: ' . date($f_datetime,$ed_stamp) . '<br />';
…will produce this output:
Start date
original: 20151217T00:00:00
timestamp: 1450306800
formatted: 2015/12/17 00:00:00
End date
original: 20161217T00:00:00
timestamp: 1481929200
formatted: 2016/12/17 00:00:00
Assuming that you have the current start and end dates stored in $startDate and $endDate variables...
$formattedStartDate = date("Y/m/d H:i:s", strtotime($startDate));
$formattedEndDate = date("Y/m/d H:i:s", strtotime($endDate));
This makes use of the date() and strtotime() functions
As far as the "T" goes...
Note that the "T" appears literally in the string, to indicate the
beginning of the time element, as specified in ISO 8601.
https://www.w3.org/TR/NOTE-datetime-970915
You can format the time using date function.
date('YYYY/MM/DD H:M:S',strtotime('$date_value'));
This uses DateTime with the createFromFormat function.
For more information, click here.
Requires PHP 5 >= 5.3.0, PHP 7
//infusionsoft format
$startDate = '20151217T00:00:00';
$endDate = '20161217T00:00:00';
//create DateTime object
$startDateTime = DateTime::createFromFormat('Ymd\TH:i:s', $startDate);
$endDateTime = DateTime::createFromFormat('Ymd\TH:i:s', $endDate);
//get DateTime with specified format
$newStartTime = $startDateTime->format('Y/m/d H:i:s');
$newEndTime = $endDateTime->format('Y/m/d H:i:s');

PHP Date not sticking

I'm having a hard time getting dates to stick in variables. When I when I'm tyring to subtract months from a current date it is giving me 0 back (defaulting back to 1-1-1970).
Any thoughts on what I could be doing wrong?
PHP:
$progress = 5;
$initialDate = date('m-d-Y');
echo "ini date: " . date('m-01-Y',$initialDate) . "<br>";
$date = date('m-01-Y', strtotime("-$progress months", strtotime(date('m-d-Y',$initialDate))));
echo "date: " . $date . "<br>";
output:
ini date: 12-01-2014
date: 08-01-1969
The 2nd argument to date() must be in internal Timestamp format, i.e. "number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)".
strtotime() is really intended to convert a user input into internal date format. If you want to do math with dates, it is better to use a more precise method. I would suggest reading up on date_add() here: http://php.net/manual/en/datetime.add.php
However, the simplest fix to your code is this:
$progress = 5;
$initialDate = time(); // current timestamp, including seconds
echo "initialDate: $initialDate<br>";
echo "ini date: " . date('m-01-Y',$initialDate) . "<br>";
$date = date('m-d-Y', strtotime("-$progress months", strtotime(date('Y-01-d',$initialDate))));
echo "date: " . $date . "<br>";
Note that I used Y-01-d format for the date fed to strtotime, to go back to the 1st of the month before doing the date math, for two reasons:
Y-m-d format has no danger of being interpreted incorrectly by strtotime, unlike m/d/y and d/m/y.
If you are going back 5 months from Jul 30 to Feb 1, it is safer to have Jul 1 as the intermediate step, not Feb 28. "Safer" meaning you don't even need to test if strtotime handles Feb 28 properly.
You can do this using the DateTime object as well. I find it a little more reliable myself. You basically just initiate a DateTime object and then use its ::sub function to subtract a DateInterval in the amount of $progress months.
$progress = 5;
$initialDate = new DateTime(date("Y-m-01"));
echo "ini date: ".$initialDate->format("m-01-Y")."<br />";
$date = $initialDate->sub(new DateInterval("P".$progress."M"));
echo "date: ".$date->format("m-01-Y")."<br />";
Output:
ini date: 12-01-2014
date: 07-01-2014

strtotime not converting correct year [duplicate]

This question already has an answer here:
php strtotime() messes with date of a different year
(1 answer)
Closed 9 years ago.
I am trying to convert a date using pickadate.js mixed with PHP using Laravel 4. The issue I am having is when I attempt to save dates that are > 2013. If I choose Jan 23th 2014 it will save as Jan 23th 2013.
This is what's being sent via $_POST to the date variable.
23 January, 2014
This is my setup
$date = strtotime($scheduler['date']);
Converts to UNIX 1358993640 (which reads Jan 23 2013)
$dateFormat = date('l: F d, Y',$date);
Which becomes:
Wednesday: January 23, 2013
Is there another function I could use? Or do I need to convert the time another way before strtotime? It works as long as it's 2013. So I am thinking once it hits 2014 it will work then also.
Assuming the textual date was created according to a fixed format, you can use DateTime::createFromFormat instead:
$date = DateTime::createFromFormat('d F, Y', $scheduler['date']);
echo $date->format('l: F d, Y');
Alternatively, just omit the comma:
echo date('l: F d, Y', strtotime('23 January 2014'));
Very strange. But found a fix. Testing on my local setup running PHP 5.5, it seems that the comma is what is causing the issue. So stripping out commas from the entered data produces the desired results with your test code:
// Set the test data.
$test_data = '23 January, 2014';
// Filter out commas from the '$test_data'
$test_data = preg_replace('/,/', '', $test_data);
// Get the Unix datetime from the test data.
$date = strtotime($test_data);
// Format the Unix datetime.
$dateFormat = date('l: F d, Y', $date);
// Output for debugging.
echo 'date: ' . $date . '<br />';
echo 'dateFormat: ' . $dateFormat . '<br />';
The output I get is:
date: 1390453200
dateFormat: Thursday: January 23, 2014

need help converting time string to usable format

I have a string in the following format
yyyy-mm-dd-hh-mm-ss
The reason its in this format is because its part of a URL. I need help converting it to a usable time format in PHP.
Here is what I have tried so far.
$time = "2013-04-14-23-33-17";
$time2 = strtotime($time);
$time3 = date('M d, Y', $time2) . ' ' . _('at') . ' ' . date('h:i a', $time2);
echo $time3;
It just echos out Jan 01, 1970 at 12:00 am which is wrong instead of Apr 14, 2013 at 11:33 pm.
It doesn't work because that's not a valid date format. See Supported Date and Time Formats.
What you need to do is create a DateTime object by specifing a format for your string.
DateTime::createFromFormat does exactly that.
You could use DateTime to help you with the parsing:
$date = DateTime::createFromFormat('Y-m-d-H-i-s', $time);
echo $date->format('M d, Y h:i a');

Display mysql datetime in human readable format with php?

Am saving to a mysql table using:
$query = mysql_query("INSERT INTO test SET postit='$postit',postdate=NOW()");
I'm then trying to display it using:
echo "<li>" . date("D, d M y H:i:s O",$row['timestamp']) . " - " . $row['postit'] . "</li>";
It's saving the correct time in the database, however it renders:
Thu, 01 Jan 70 01:00:00 +0100
Anyone point out the stupidity?
The PHP date() function uses a Unix timestamp as the second variable in the function. What you are passing to the function is a MySQL time stamp. Try using:
echo date("D, d M y H:i:s O",strtotime($row['timestamp']));
I always like to use this function for that:
function parse_sql_timestamp($timestamp, $format = 'd-m-Y')
{
$date = new DateTime($timestamp);
return $date->format($format);
}
This way we can even go beyond 2038 ;)

Categories