PHP String to time Error - php

I've been using string to time in my website to format the date stamps held in a database and up until now the method I'm using has been fine up to this point. Here is the code:
$memberdate = $member['date'];
$memberdate = date('jS F Y', strtotime($memberdate));
The initial $membersate value is 1381742596 - The output value is 1st January 1970
Does anyone know a reason why this would be working for all other pages/scripts but not now?

strtotime() is used, when you are converting ie. "2013-11-07 16:29:30" to its integer value.
But you already have it as unix_timestamp (integer value), so you dont need to use strttotime().
$memberdate = date('jS F Y', $member['date']);

Related

strtotime() always returns same day of month but gets other parts of date correct

I have a part of a simple web app that takes input from a JavaScript calendar picker, sends it to the server, and then the server converts it to a human readable time and echos it back out.
My HTML form ends up having a value formatted as MM/DD/YYYY.
When this gets POSTed to the server this PHP transforms it into a differet format (please note that I'm using CodeIgniter so $this->input->post() is the same as $_POST[]):
php
$date = date('l, F n, Y', strtotime($this->input->post('date')));
Example input and output
HTML text input will get a value of "04/21/2013".
PHP's strtotime() will echo back "Sunday, April 4, 2013".
No matter what date I put in there, strtotime() always gives me the correct date back except for the day of the month which always ends up being the same number as the number of the month (for example, any dates in May become "May 5, 2013" and so on).
Update: Solved
As soon as I posted this I realized it was the 'n' in 'l, F n, Y' that caused the issue. Turning it to a 'j' fixed things. Sorry to waste everyone's time.
Use j for day of the month, not n which is the numeric month:
php $date = date('l, F j, Y', strtotime($this->input->post('date')));
See it in action
See the documentation for date() here: http://php.net/manual/en/function.date.php
Change the n in the first parameter of your date function to j and you will get the number of the day of the month.

Convert specific string to time in PHP

I need to convert a string into date format, but it's returning a weird error. The string is this:
21 nov 2012
I used:
$time = strtotime('d M Y', $string);
PHP returned the error:
Notice: A non well formed numeric value encountered in index.php on line 11
What am I missing here?
You're calling the function completely wrong. Just pass it
$time = strtotime('21 nov 2012')
The 2nd argument is for passing in a timestamp that the new time is relative to. It defaults to time().
Edit: That will return a unix timestamp. If you want to then format it, pass your new timestamp to the date function.
To convert a date string to a different format:
<?php echo date('d M Y', strtotime($string));?>
strtotime parses a string returns the UNIX timestamp represented. date converts a UNIX timestamp (or the current system time, if no timestamp is provided) into the specified format. So, to reformat a date string you need to pass it through strtotime and then pass the returned UNIX timestamp as the second argument for the date function. The first argument to date is a template for the format you want.
Click here for more details about date format options.
You are using the wrong function, strtotime only return the amount of seconds since epoch, it does not format the date.
Try doing:
$time = date('d M Y', strtotime($string));
For more complex string, use:
$datetime = DateTime::createFromFormat("d M Y H:i:s", $your_string_here);
$timestamp = $datetime->getTimestamp();

How do I convert this timestamp to PHP? Bing Maps API

I've been playing around with the Bing API using json and PHP. The array spits out the following for dates:
[end] => /Date(1354867200000)/
[lastModified] => /Date(1349441488000)/
I thought this was a unix timestamp, but it I don't think it is. What I did was a preg_replace like this
$last_updated = $resource->lastModified;
$last_updated_timestamp = preg_replace('/[^0-9.]*/','',$last_updated);
Then tried to convert it to a date
$last_updated_date = date('l F d Y g:i:s A',$last_updated_timestamp);
The results that it's showing me for date range back from the year 1967 to 2000. Is this a different kind of timestamp that I don't know of? If so, how do I correct this? Any help would be appreciated!
The number part is milliseconds-since-the-Epoch (January 1, 1970 at midnight — the milliseconds version of a unix timestamp). This is a fairly conventional way to represent dates in JSON (since JSON doesn't have a date type).
So getdate(theNumber / 1000) will give you the date (since getdate expects seconds, not milliseconds, since The Epoch).
If what you want to do is convert a unix timestamp to date format, you can do it by doing the following:
date("F j, Y g:i a", strtotime($unix_timestamp));
Where $unix_timestamp is your unix timestamp in this case.
You can always print it out for testing purposes by adding echo before it.
So in this case it could be:
$last_updated_date = date("F j, Y g:i a", strtotime($last_updated_timestamp));

UNIX Time stamp and MYSQL

I am storing the date of entry in mysql via TIMESTAMP and default value UNIX_TIMESTAMP, however when I bring it back it's 2011-08-16 11:43:52 and if I try to style it with
<?= date('F j, o', $a['time']) ?>
It just does the timestamp from zero, bringing back December 31, 1970
Why? And how can I fix it?
Use
SELECT UNIX_TIMESTAMP(field)
When UNIX_TIMESTAMP() is used on a TIMESTAMP column, the function returns the internal timestamp value directly, with no implicit “string-to-Unix-timestamp” conversion.
Manual
A MySQL TIMSTAMP column is stored as YYYY-MM-DD HH-MM-SS, even if you input a UNIX_TIMESTAMP it will still be stored in that format.
So what you can do is:
<?= date('F j, o', strtotime($a['time'])) ?>
Demo: http://codepad.org/jBLR2KpH
date() takes a number and not a string. MySQL always returns your date as a string regardless of its internal representation. Try this:
<?php echo date("F j, o", strtotime($a['time']); ?>

convert mysql timestamp into actual date and time?

I have dates stored in a mysql table, they are set to store as CURRENT TIMESTAMP in each row and are stored as follows:
2010-05-29 01:17:35
but what i am trying to do is somehow use PHP to be able to seperate everything and create a date more like:
May 29 2010 1:17 AM
can anyone at least direct me in the right path that i should take. any help is greatly appreciated!
echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35'));
http://php.net/manual/en/function.date.php
You have two solutions :
Use strtotime() to parse the date to a timestamp, and date() to re-format it to a string
Or use the DateTime class
In PHP code, this would mean using :
echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35'));
Or :
$dt = new DateTime('2010-05-29 01:17:35');
echo $dt->format('M j Y g:i A');
strtotime + date is the solution you'll see used the most ; but it is not the best solution : with those, you'll work on UNIX Timestamps, which means a limited range of dates (from 1970 to 2038, if using 32 bits integers).
ON the other hand, using the DateTime class, there will be no limit to the range of dates you can work with.
If you have a DATETIME field in your table and you only want the date field, then:
SELECT DATE_FORMAT(timestamp,'%M %D, %Y') FROM Mytable;
or:
SELECT DATE_FORMAT(timestamp,'%Y-%m-%d') FROM Mytable;
where timestamp is your column name.

Categories