UNIX Time stamp and MYSQL - php

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

Related

PHP String to time Error

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

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

PHP date format error

I have an array with a key timestamp with the following content
"timestamp" => "2011-11-29 00:00:00"
When i try to change the format using this
date("F j, Y", $data['Visitor']['timestamp']);
i get the following error
A non well formed numeric value encountered
You should be using the strtotime on the datetime data to convert it into Unix timestamp first.
date("F j, Y", strtotime($data['Visitor']['timestamp']));
Checkout the documentation of date it accept a Unix timestamp as a second parameter and you are passing a datetime value.
DEMO
The function requires the Unix Time which is numeric - and not a string formatted date.
As #Shakti Singh mentions you should use strtotime for that.
From the PHP docs on the timestamp parameter:
The optional timestamp parameter is an integer Unix timestamp that
defaults to the current local time if a timestamp is not given. In
other words, it defaults to the value of time().
A newer way to do this as of PHP 5.2 is the DateTime class:
$datetime = new DateTime('2011-11-29 00:00:00');
echo $datetime->format('F j, Y');
See it in action

Date and Time functions in PHP & MySQL

I have date and time stored in my database as MySQL DATETIME datatype. While inserting into the database, I am using the following PHP variable
$serverTime = strftime("%Y-%m-%d %H:%M:%S",$_SERVER['REQUEST_TIME']);
A sample DATETIME stored in database is: 2011-11-26 01:00:27
Now my website needs the date to be displayed in the following format: November 26, 2011 (time is not required)
How can I do it? I am trying the below in PHP but wrong output I am getting.
echo strftime("%B %d, %Y",$serverTime)
The PHP strftime() function wants input to be a 32-bit integer timestamp, which is the number of seconds since 1970-01-01 00:00:00.
The default output format for MySQL datetime is YYYY-MM-DD HH:MM:SS. When you use this string in PHP, the leading digits are converted into the integer 2011, which is interpreted to be within the first hour of 1970-01-01.
So you must fetch the datetime from MySQL in another format. Choices:
UNIX_TIMESTAMP(datetime_column) to fetch the datetime as an integer that you can give to PHP's strftime() function.
DATE_FORMAT('datetime_column', '%M %d, %Y') to format the datetime as a string, and then you don't have to use strftime() to format it.
While I would recommend a completely SQL approach, you can use PHP's DateTime class to process your string in PHP (assuming you're running PHP > 5.3):
// Pick a valid TimeZone
$date_obj = DateTime::createFromFormat( 'Y-m-d H:i:s', '2011-11-26 01:00:27', new DateTimeZone( 'America/New_York'));
echo $date_obj->format('F d, Y');
Demo

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