unable to get date and time from timestamp in php? [duplicate] - php

This question already has answers here:
Why don't PHP and Javascript's timestamps match?
(3 answers)
Closed 6 years ago.
I have written the following code:
$timestamp = "1483625713000";
$date = date('d-m-Y', $timestamp);
$time = date('Gi.s', $timestamp);
but the output is not coming as expected:
My outputs:
echo $date // 23-03-48984
echo $time // 2136.40

Looks like you've got milliseconds in that timestamp. Try this:
$timestamp = 1483625713000 / 1000;
$date = date('d-m-Y', $timestamp);
$time = date('Gi.s', $timestamp);
var_dump($date);
var_dump($time);
Outputs
string(10) "05-01-2017"
string(7) "1415.13"

Related

PHP - convert midnight date into timestamp [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I'm having trouble converting midnight date into timestamp. I'm getting via AJAX POST method timestamp (including time zone offset) from klient, so then I'm converting this to a midnight date like this:
$timestamp = 1463990400; // for example
echo date('d-m-Y H:i:s', strtotime('today', $timestamp));
This line output is: 23-05-2016 00:00:00
And I would love to convert this midnight date time into timestamp to create SQL SELECT.
Is there any solution?
Finally found way how to solve my problem with mktime() function:
$offset = date('d-m-Y H:i:s', strtotime('today', $datum_to));
$parts = preg_split('/\s+/', $offset);
$date_convert = explode("-", $parts[0]);
$time_convert = explode(":", $parts[1]);
$this_tz_str = date_default_timezone_get();
$this_tz = new DateTimeZone($this_tz_str);
$now = new DateTime("now", $this_tz);
$offset = $this_tz->getOffset($now);
$second_starttimeUTC = mktime(0, 0, 0, $date_convert[1], $date_convert[0], $date_convert[2])+$offset;

PHP Convert from 'd/m/Y - h:i:s A' to timestamp [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I want to convert format d/m/Y - h:i:s A to a timestamp in PHP.
I tried:
$time = '26/07/2014 - 03:59:23 PM';
$time2 = strtotime($time);
But that returns null
This is easiest when using the DateTime class:
$dateString = '26/07/2014 - 03:59:23 PM';
$datetime = DateTime::createFromFormat('d/m/Y - h:i:s A', $dateString);
echo $datetime->getTimestamp();
An example of this code is here.

Format date using php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a PHP variable '$date', and it's value is: y/m/d h:i:s format.
e.g: $date = "2014-11-10 17:25:00";
I want to change this format to m/d/y h:i:s format, so that I'm using PHP code below. It's working fine but is there any PHP function to do it in one line of code ?
$date = "2014-11-10 17:25:00";
$explode = explode(" ", $date);
$part1 = $explode[0];
$part2 = $explode[1];
$explode = explode("-", $part1);
$year = $explode[0];
$month = $explode[1];
$day = $explode[2];
$created_date = "$month-$day-$year $part2";
You can use date() & strtotime() functions. Example:
$date = "2014-11-10 17:25:00";
echo date('m-d-Y H:i:s', strtotime($date));
Output:
11-10-2014 17:25:00

Add minutes to time [duplicate]

This question already has answers here:
Adding 30 minutes to time formatted as H:i in PHP
(7 answers)
Closed 9 years ago.
I have the following code
$time = "12:00";
$duration = 90 . " minutes";
$arrival = strtotime("+" . $duration, $time);
Output should be 13:30.
I get the following error: "A non well formed numeric value encountered in" (line with $arrival)
What can I do?
The following code should work:
$time = strtotime("12:00");
$duration = "+90 minutes";
$arrival = strtotime($duration, $time);
print(date("H:i", $arrival));
Demo: https://eval.in/95328
Read more about Unix timestamp and the PHP function strtotime.
Consider using PHP DateTime and DateInterval classes, like this:
$time = new \DateTime('now');
$time->add( new \DateInterval('PT90M') );
Try This Simple code :
<?php $time = strtotime('12:00');
$more = date("H:i", strtotime('+90 minutes', $time)); ?>

Format string according to date format [duplicate]

This question already has answers here:
How do I convert an ISO8601 date to another format in PHP?
(2 answers)
Closed 8 years ago.
There is a string:
$time = "2013-07-10T21:00:00.000";
How to convert it to the format "2013-07-10 21:00:00"?
$time = strtotime("2013-07-10T21:00:00.000");
return strftime("%Y-%m-%d %H:%M:%S", $time);
$date = new \DateTime($time);
$date->format('Y-m-d H:i:s');
$time = "2013-07-10T21:00:00.000";
echo date("Y-i-d H:m:s",strtotime($time));

Categories