converting datetime in mysql with php [duplicate] - php

This question already has answers here:
Converting a datetime field timestamp from mysql to php
(2 answers)
Closed 9 years ago.
I have the following DateTime called 'StartTime' field in mysql with '1899-12-30 12:17:52'
I am trying to convert it to a time on the screen for the users with php using
date('h:i:s A',strtotime($row_get_student_times['StartTime']))
but for whatever reason it always returns 4:00 PM no matter what the time. Any clue why this is happening?

It's because you are trying to show a date before the uinx epoch (1970).
Try this:
$date = new DateTime($row_get_student_times['StartTime']);
echo $date->format("h:i:s A");

You need to use DateTime:
$datetime = new DateTime($row_get_student_times['StartTime']);
echo $datetime->format('h:i:s A');

<?php
$dateTimeString = '1899-12-30 12:17:52';
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s', $dateTimeString);
echo $dateTime->format('H:i:s A');

Related

Convert database timestamp to date [duplicate]

This question already has answers here:
Formatting an SQL timestamp with PHP
(6 answers)
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a database timestamp with this format 2016-06-01 11:46:00 and I would like to convert and echo it to this format Wed. 01-06-2016.I tried a lot but no desirable results..Any ideas?
Thanks a lot, it worked!
If I want the difference between today and the past date in days or weeks I think I use date_diff.How exactly?
Simply use date and strtotime:
$time = '2016-06-01 11:46:00';
echo date("D. d-m-Y", strtotime($time)); //Wed. 01-06-2016
Updates:
$grk = array("Tet"); // complete the rest of the array
$eng = array("Wed"); // complete the rest of the array
$time = '2016-06-01 11:46:00';
$date = date("D. d-m-Y", strtotime($time));
echo str_replace($eng, $grk, $date);
$time = '2016-06-01 11:46:00';
echo date("D. d-m-Y", strtotime($time)); //Wed. 01-06-2016

Convert date and time format in passed date value from db [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a date value from the database and its format is: YYYY-mm-dd hh:mm:ss
I want to convert it to this format: YYYY/mm/dd hh:mm
Here is my code:
<?php
$datetime = $value['last_online']; //from db
$date = date('d-m-Y', $datetime);
$time = date('Gi.s', $datetime);
echo $date.' '.$time;
?>
But it's not working and is giving me an error of:
A non well formed numeric value encountered
I want to get rid of the minus signs and the seconds so my new date and time format would be YYYY/mm/dd hh:mm. How do I do it?
Any ideas?
Use like below
$datetime = $value['last_online']; //from db
$date = date('Y/m/d h:i', strtotime($datetime));

Convert yyyy/mm/dd-hh-mm-ss.00 to Y-m-d H:i:s in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I'm developing a system that recibe a date format like: yyyy/mm/dd-hh-mm-ss.00 and I need convert it to Y-m-d H:i:s.
What I done was:
$newDate = date("Y-m-d H:i:s", strtotime($values[2]));
where $values[2] it is the date in that format, for instace: 2014/01/01-13-18-23.00. But the result is: 1970-01-01 01:00:00
Can you help me?
Thanks
You can try with strptime like
$format = "%Y-%m-%d %H:%i:%s";
echo strptime($values[2], $format);
Try using DateTime
$date = DateTime::createFromFormat('Y/m/d-H-i-s.00', '2014/01/01-13-18-23.00');
echo $date->format('Y-m-d H:i:s');
See demo here
You should be able to do something like this:
$date = DateTime::createFromFormat("Y/M/d-H-i-s.00", $values[2]);
echo $date->format("Y-m-d H:i:s");
The dateTime format for strtotime() function are predefined and you have to follow the rules to get desired result.
Mixed date time formats are available here PHP: Compound Formats - Manual

extract date from datatime in php or Cakephp [duplicate]

This question already has answers here:
How to get time and date from datetime stamp in PHP?
(7 answers)
Closed 9 years ago.
i have a table in my cakephp which have a field name datetime and datatype is datetime
storing in this format
2013-06-18 00:00:00
I need to extract the date part of the value not the time ..
i extract time like this
$dateTime = $recentCall['Calllog']['dateTime'];
$time = date("H:i:s",strtotime($datetime));
now i want to extract the date part.which i dont know how can i do this .. i have done some research but nothing works out for me
This isn't exactly a CakePHP answer, but a PHP one.
$dateTime = $recentCall['Calllog']['dateTime'];
$timestamp = strtotime($dateTime);
$date = date('Y-m-d' , $timestamp);
$time = date('H:i:s' , $timestamp);
If you want to do somethink more Cakeish and use the CakePHP wrapper, should be:
$dateTime = $recentCall['Calllog']['dateTime'];
$timestamp = CakeTime::fromString($dateTime);
$date = CakeTime::format($dateTime, 'Y-m-d');
$time = CakeTime::format($dateTime, 'H:i:s');
I hope my answer is clear.
Ref: http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html
Use Y-m-d as first parameter in date to extract date.
$date = date("Y-m-d",strtotime($datetime));

converting a SQL server date time to mysql date time [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert DateTime to VarChar
Example
How do you convert a SQL datetime (for example 08/14/2012 1:05:18 PM ) to the mysql way ( that is Y-m-d H:i:s )?
Depends on where you are wanting to convert it.
To convert in your SQL directly:
convert(varchar(23), getdate(), 121)
PHP using the date and strtotime functions:
date("Y-m-d H:i:s ", strtotime("08/14/2012 1:05:18 PM"));
Change the format of the date in PHP like so:
<?
$date = date("Y-m-d H:i:s ", strtotime("08/14/2012 1:05:18 PM"));
echo $date;
?>
$var = "08/14/2012 1:05:18 PM";
echo date('Y-m-d H:i:s', strtotime($var));

Categories