Trying to convert mysql timestamp to time using strtotime - php

strtotime($_SESSION['starttime'])
I tried to convert the mysql time stored as $_SESSION['starttime'] of value 1422094831 but I want to convert the resulting to time using strtotime but returns no value. What could be wrong?

strtotime() is used to create a timestamp, not convert it to a date. If your session variable stores a timestamp, try using the date() function to convert it to a more readable format.
echo date("Y-m-d", $yourTimestamp); //Will print YYYY-MM-DD format date
The PHP doc will help you using the date() function correctly.
If that's not what you want to do then please provide more information in your question, from what I understand you already have a timestamp.

Related

PHP: Convert epoch to MySQL DateTime format

I have to store epoch timestamps into a MySQL database DateTime column. I need to be able to convert the epoch into the DateTime form to be able to add it to the database. The epoch data is grabbed from an external source so we have no control over that, the database is also established and should be left as it is. We just need to be able to convert between the two in PHP.
PHP
date("Y-m-d H:i:s",$epochTS);
MySQL
FROM_UNIXTIME(timestampColumn)
Nothing more to it
No need to do any convert for MySQL or PHP just or enter the same to database
echo date('r', $epoch);
strotime() is the function used to convert datetime to epoch. Documentation
strotime() is the function used to convert datetime to epoch. Documentation
time() is the function used to convert epoch to datetime. Documentation

How to save data in a different format in MySQL database

I notice that MySQL save date as 0000-00-00 format. But in my application I have validate date for this 00-00-0000 format. I notice that date is not saving properly as different formats. Can I change mysql data format to 00-00-0000 format?
The 00-00-0000 format is not entirely clear; it could be dd-mm-yyyy for instance.
You can simply convert the date you have like so:
$mydate = '24-12-2012';
// create date object by using a known format
// see manual page for all format specifiers
$d = DateTime::createFromFormat('d-m-Y', $mydate);
// format date object in yyyy-mm-dd
echo $d->format('Y-m-d');
See also: DateTime::createFromFormat() DateTime::format()
You can also use MySQL str_to_date function
INSERT INTO yourtable (datefield) VALUES (str_to_date('01-02-2007', '%d-%m-%Y'));
As #Jack explained it is one of the correct way you can choose and
at the time of select date from database you can use
DATE_FORMAT(date,format).
You can't.
Instead of that you have to convert your custom format to database format and back.
To store it in the database you can use explode PHP function.
To when selecting, you can format mysql date format using DATE_FORMAT mysql function.

How to deal with DATE-type in MYSQL & PHP?

I'm building a website with php and i'm using the DATE-type in my MYSQL table to store dates. The problem that i have is that this stores the dates by default in the format YYYY-MM-DD. But i need this format DD-MM-YYYY to appear on my PHP page with the possibility of calculating the amount of days between 2 different dates. How can this be achieved?
That's a display problem. Use mysql's date_format() function to convert to whatever your display requirements are, e.g.
SELECT date_format(datefield, '%d-%m-%Y'), ...
You can use strtotime to convert a string representation of the date to an actual date object in php, then use the date function to spit out the date as any string format you wish. Also, you can be strtotime to perform date calculations. Additional information can be found at this blog post.
$phpDate = strtotime($stringDateFromDb);
date('d-m-y', $dateFromDb);
strtotime('-3 days', strtotime($stringDateFromDb));
Here is an example for a way to do it:
$date_str = '2012-05-20'; //you get it from db
$date_now = strtotime($date_str); //convert it to unix timestamp
$yesterday=$date_now-24*60*60; //make calculations
echo 'yesterday was: '. date('d-m-Y',$yesterday); //date() returns the date in format you need
Further example here: How to calculate the difference between two dates using PHP?

Is there any PHP function to format time (not date)?

I have a mysql table field set as time type which stores data in the HH:mm:ss format. So when I list the data, it prints as, for example, 16:30:00. But I want to display hh:mm part only (not the seconds).
In case of datetime types, I can do date('H:i', '2010-03-16 16:30:00'). I mean I can retrieve any part. I wonder if there is any similar way like this for time only fields??
Please see, I can manipulate the time string to get rid of seconds in time part using str_replace, explode etc, I just wonder if there is any standard function there which I am not aware of.
If you want to do this with PHP, you'd have to get a timestamp from the time first, e.g.
echo date('H:i', strtotime('16:30:00'));
will output 16:30. Strtotime will assume the time is for the current date then.
You can let MySQL return the data in the format you want using Date_Format()
edit: as fireeyedboy pointed out there's also a TIME_FORMAT() function.
Another approach is:
SELECT UNIX_TIMESTAMP(time_field) AS tstamp FROM table;
Then you can use PHP's date() function to format it.
$time = date('H:i', $tstamp);

Format DATETIME column using PHP after printing

Hey, I am using DATETIME for my column for my database. I am trying to find the best way to format this date held in the row using PHP. I tried using date() but that would just print the current date. My row looks something like:
$row['date_added']
Any ideas?
A couple options.
Use MySQL's DATE_FORMAT function to format it.
Use MySQL's UNIX_TIMESTAMP function to get a timestamp PHP's date() function can play with.
Run the MySQL DATETIME through PHP's strtotime() function to get a timestamp PHP's date() function can play with.
As an example of #3:
print date('<format string>', strtotime($row['date_added']));
Just follow the documentation on date() to construct a format string to your liking and swap it in.

Categories