PHP - convert midnight date into timestamp [duplicate] - php

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;

Related

convert string date to timestamp and timestamp to date with fixed result [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have an string date like this: 2015/12/20 13:58:59
I try to convert timestamp:
$idate = $user->multiexplode(array("/"," ",":"),strip_tags("2015/12/20 13:58:59"));
//mktime( $hour , $minute , $second , $month , $day , $year , $is_dst );
$timestamp = mktime($idate[3],$idate[4],$idate[5],$idate[1],$idate[2],$idate[0]);
And now I try to convert real date:
echo 'new date: '.jdate('Y/n/j H:i:s',$timestamp);
Ok...it works but there is a problem!
according of time server,I get variable time.
for examle for location +1 GMT: 2015/12/20 14:58:59
for -1 GMT: 2015/12/20 11:58:59
I want for all server print 2015/12/20 13:58:59 again
You can use DateTime class and it's methods to convert the date and time to unix timestamp. And use strftime() to convert the unix timestamp back to your desired format, like this:
$datetime = "2015/12/20 13:58:59";
// covert timestamp
$unixdatetime = DateTime::createFromFormat('Y/m/d H:i:s', $datetime)->getTimestamp();
echo $unixdatetime . "<br />"; // 1450616339
// now format the unix timestamp
$formatted_datetime = strftime("%Y/%m/%d, %H:%M:%S",$unixdatetime);
echo $formatted_datetime; // 2015/12/20, 13:58:59
Output:
1450616339
2015/12/20, 13:58:59
Here are the references:
DateTime class
strftime()
Use this code.
<?php
$date = '2015/12/20 13:58:59';
//convert to timestamp
$timestamp = strtotime($date);
echo $timestamp;
//convert timestamp back to datetime
$date_again = date('Y/m/d H:i:s', $timestamp);
echo $date_again;
?>

PHP: Date Formatting - MM/DD/YY > YYYY/MM/DD [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
Hi I am trying to get some PHP date formatting resolved. The problem I am having is random unexpected results being returned instead of the correct date after I try and format them.
I have the date MM/DD/YY passed in as a string and I am wondering how to convert that to a YYYY/MM/DD. When it try to convert the date it is like it is staying in the same place but trying to convert the individual section so the MM (12) goes to the year YYYY (2012)
Here is the section of code I have been using to try and change the format:
$date = $_GET["datepicker"];
$arr[$i] = strftime("%Y-%m-%d", strtotime($date));
The $arr[$i] is just the array I am putting it into this shouldn't affect anything.
I have also tried the following:
$arr[$i] = date("Y-m-d", strtotime($date));
Thanks,
Kieran
The easiest way to get the output you need is to use the DateTime class, in particular: DateTime::createFromFormat:
$date = DateTime::createFromFormat('m/j/y', $_GET['datepicker']);
//now to get the outpu:
$arr[$i] = $date->format('Y-m-d');
There is a procedural-style alternative, too:
$date = date_create_from_format('m/j/y', $_GET['datepicker']);
//note, date_create_from_format returns an instance of DateTime
$arr[$i] = date_format($date, 'Y-m-d');//same as $date->format('Y-m-d');
You can use mktime:
$date = $_GET["datepicker"];
list($month, $day, $year) = explode('/', $date);
$timestamp = mktime(0, 0, 0, $month, $day, $year);
$output = date('Y-m-d', $timestamp)

find the last day/date of a WEEKOFYEAR with php [duplicate]

This question already has answers here:
How to get the first day of a given week number in PHP (multi-platform)?
(9 answers)
Closed 9 years ago.
I have a value that is the number for the weekofyear (between 1-52 ). I want to find out the date (yyyy-mm-dd) for the last day of that week.
I would prefer to do it in PHP rathe then MYSQL.
This can be easily solved with DateTime::setISODate() method:
$week = 50;
$dt = new DateTime();
$dt->setISODate($dt->format('o'), $week, 7);
echo $dt->format('Y-m-d');
demo
Or you can just create DateTime object (or unix timestamp with strtotime()) with ISO-8601 format like 2014-W50-7:
$week = 50;
$iso = sprintf("2014-W%02d-7", $week);
$dt = new DateTime($iso);
echo $dt->format('Y-m-d');
echo date('Y-m-d', strtotime($iso)); # or using strtotime()
demo

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

Generating week start and end dates with timezone offset [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert time and date from one time zone to another in PHP
I have two variables which show the weeks start and end date as follows:
$week = strtotime('-' . date('w') . ' days');
$start = date('Y-m-d', $week);
$end = date('Y-m-d', strtotime('+6 days', $week));
How can I offset these dates for a different timezone (e.g. PST?)
This will convert a tie in your servers timezone to the given timezone.
$datetime = new DateTime;
$newTZ = new DateTimeZone('Your/TimezoneHere');
$datetime->setTimezone($newTZ);

Categories